Iterator
In one line
An iterator is an object that provides a sequence of values one at a time via a next() method.
In simple words
An iterator is an object that produces a sequence of values on demand. It implements the iterator protocol: an object with a next() method that returns { value, done } — value is the next item and done is true when the sequence is exhausted.
Arrays, strings, Maps, and Sets are all iterable — they have a default iterator accessible via [Symbol.iterator]. The for...of loop uses iterators internally. You can create custom iterators by defining an object with a next() method.
Iterators enable lazy evaluation — values are produced one at a time, which is memory-efficient for large or infinite sequences. Generators (function*) are a convenient way to create iterators.
Example
const arr = [1, 2, 3];
const it = arr[Symbol.iterator]();
console.log(it.next()); // { value: 1, done: false }
console.log(it.next()); // { value: 2, done: false }
console.log(it.next()); // { value: 3, done: false }
console.log(it.next()); // { value: undefined, done: true }An iterator's next() returns each value in sequence, then done:true when exhausted.
Related terms
Related Resources
Related Lessons
- Arrays
Learn about iteration.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.