- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript Iterators
JavaScript Generators & Iterators
JavaScript Iterators
An iterator is an object with a
nextmethod that hands out one value at a time.
Arrays, strings, Maps and Sets all work with for of because they follow this same protocol underneath.
Seeing it directly explains what a loop is really doing.
Example
Example
javascript
const numbers = [1, 2, 3];
const iterator = numbers[Symbol.iterator]();
console.log(iterator.next());
console.log(iterator.next());