Created
June 9, 2018 20:08
-
-
Save nodlAndHodl/1f8878a9531a21f58399a507346e8056 to your computer and use it in GitHub Desktop.
Using an iterator to get values from object or array in ES6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Showing the difference between using a 'for' loop and 'for of' loop in ES6 | |
let numbs = [1,2,3,4,5]; | |
sum = 0; | |
for (let i = 0; 0 > numbs.length; i++){ | |
sum += numbs[i]; | |
} | |
console.log(sum); //15 | |
sum = 0; | |
//the 'for of' loop in ES6 is actually using an iterator. | |
//numbs[Symbol.iterator](); | |
//also great for parsing over more complex data structures that are iterable. | |
for (let i of numbs){ | |
sum += i; //direct access to object in array | |
} | |
console.log(sum); //15 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment