Iterators
The Iterators are objects with a special structure in JavaScript. They must have a next() method that returns an object with the value and done properties. The value property represents the next value in the sequence and the done property indicates whether there are more values to be iterated. The Iterators are commonly used for iterating over data structures like arrays, maps, and sets.
Example: In this example, we will see an iterator looping through an array.
Javascript
const colors = [ 'red' , 'green' , 'blue' ]; const GFG = colors[Symbol.iterator](); console.log(GFG.next()); console.log(GFG.next()); console.log(GFG.next()); console.log(GFG.next()); |
Output
{ value: 'red', done: false } { value: 'green', done: false } { value: 'blue', done: false } { value: undefined, done: true }
Contact Us