Skip to content

Instantly share code, notes, and snippets.

@nodlAndHodl
Created June 9, 2018 20:08
Show Gist options
  • Save nodlAndHodl/1f8878a9531a21f58399a507346e8056 to your computer and use it in GitHub Desktop.
Save nodlAndHodl/1f8878a9531a21f58399a507346e8056 to your computer and use it in GitHub Desktop.
Using an iterator to get values from object or array in ES6
//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