iterator
An iterator is an object that produces a sequence of values one at a time, handing over the next value each time code calls its next() method.
Each call to next() returns a result object with two properties. The value property holds the current value. The done property is false while values remain and true once the sequence has ended. Any object with a next() method that follows this convention, known as the iterator protocol, is an iterator.
Iterators are stateful. Each next() call moves the iterator forward, and nothing moves it back. Once an iterator reports done: true, the protocol expects every later call to do the same, although the language doesn’t enforce it. An iterator can also compute each value only when asked, which lets it stand for a sequence with no end.
Code rarely calls next() directly. An iterable, such as an array, a string, a Map, or a Set, returns a fresh iterator from its [Symbol.iterator]() method. Then for...of, spread syntax, and array destructuring call next() to pull values from that iterator.
Built-in iterators inherit lazy helper methods such as .map() and .take() from Iterator.prototype. Generator functions, which produce their values with yield, also return iterators. An async iterator follows the same protocol, except that its next() method returns a promise of the result object.
Example
Say a music player keeps a queue of upcoming tracks and pulls one track at a time from it:
> const queue = ["Intro", "Overture", "Finale"].values();
undefined
> queue.next()
{ value: 'Intro', done: false }
> queue.next()
{ value: 'Overture', done: false }
> [...queue]
[ 'Finale' ]
> queue.next()
{ value: undefined, done: true }
> [...queue]
[]
The .values() method returns an iterator over the array, and each next() call advances it by one track. Built-in iterators are iterable too, with a [Symbol.iterator]() method that returns the iterator itself, so spread syntax accepts queue.
The spread picks up where the manual calls stopped and collects only the last track. After that, every next() call reports done: true, and a second spread produces an empty array. Spreading an iterator doesn’t restart it, so another pass over the tracks needs a new iterator from the array.
Try calling queue.next() and spreading [...queue] in any order to watch the iterator move forward through the tracks and never back:
An iterator doesn’t need a built-in behind it. Say a support desk issues ticket numbers without a fixed end:
> function createTicketIds(start) {
… let id = start;
… return {
… next() {
… id += 1;
… return { value: id, done: false };
… },
… };
… }
undefined
> const tickets = createTicketIds(1000);
undefined
> tickets.next()
{ value: 1001, done: false }
> tickets.next().value
1002
> [...tickets]
Uncaught TypeError: tickets is not iterable
The plain object returned by createTicketIds() is an iterator because its next() method follows the protocol. It never reports done: true, and it computes each number only when asked, so the sequence has no end. Spread syntax still rejects it, because it expects an iterable. The object has a next() method but no [Symbol.iterator]() method that would hand out the iterator.
Related Resources
Course
JavaScript Iterators and Generators for Pythonistas
Port a weather station's sensor feed from Python to JavaScript, and learn to write iterators and function* generators, chain lazy iterator helpers, and read async feeds with for await...of.
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Nov. 19, 2026