Iterator and Generator Fundamentals
An iterator is an object that hands out values one at a time. It has a next() method, and each call returns an object with two properties: value, which is the next value, and done, which is true once there is nothing left. A generator is a special kind of function that builds an iterator for you, so you write the sequence as ordinary code instead of tracking the state by hand.
for...of, spread syntax (...), destructuring and Array.from() all work by calling next() until done is true.
Built-in iterators
Section titled “Built-in iterators”Arrays, strings, maps and sets each have a Symbol.iterator method that returns a fresh iterator. You can call it yourself to see the protocol at work:
const todos = ['shop', 'cook', 'wash up'];const it = todos[Symbol.iterator]();
console.log(it.next());console.log(it.next());console.log(it.next());console.log(it.next());This prints:
{ value: 'shop', done: false }{ value: 'cook', done: false }{ value: 'wash up', done: false }{ value: undefined, done: true }Writing an iterator by hand
Section titled “Writing an iterator by hand”Any object with a suitable next() method is an iterator. This one counts down from a given number, and it keeps its position in a variable that the next() method closes over:
function makeCountdown(from) { let current = from; return { next() { if (current < 1) { return { value: undefined, done: true }; } return { value: current--, done: false }; }, };}
const countdown = makeCountdown(2);console.log(countdown.next());console.log(countdown.next());console.log(countdown.next());This prints:
{ value: 2, done: false }{ value: 1, done: false }{ value: undefined, done: true }Generator functions
Section titled “Generator functions”A generator function is declared with function*. Calling it does not run the body. Instead it returns a generator object, and each call to that object’s next() runs the body until the next yield, which hands out a value and pauses the function where it stands. A return ends the sequence, and its value comes back with done: true:
function* countdown(from) { for (let n = from; n > 0; n--) { yield n; } return 'lift-off';}
const gen = countdown(2);console.log(gen.next());console.log(gen.next());console.log(gen.next());console.log(gen.next());This prints:
{ value: 2, done: false }{ value: 1, done: false }{ value: 'lift-off', done: true }{ value: undefined, done: true }The generator does the same job as makeCountdown, but the loop variable replaces the hand-written state.
Generators are iterable
Section titled “Generators are iterable”A generator object is an iterator and an iterable at once, so it works directly with for...of and spread. Both of these stop as soon as done is true, which means the value from return is not included:
for (const n of countdown(3)) { console.log(n);}console.log([...countdown(4)]);This prints:
321[ 4, 3, 2, 1 ]A generator object can be used only once. After it finishes, it keeps returning { value: undefined, done: true }, so call the generator function again for a new run.
Endless sequences
Section titled “Endless sequences”Because a generator pauses at each yield, it can describe a sequence with no end. It only works out a value when asked for one:
function* idMaker() { let id = 1; while (true) { yield `order-${id++}`; }}
const ids = idMaker();console.log(ids.next().value);console.log(ids.next().value);console.log(ids.next().value);This prints:
order-1order-2order-3Do not spread an endless generator or loop over it without a break, because the loop never finishes.
Iterator helpers
Section titled “Iterator helpers”Since ES2025, iterators have their own map(), filter(), take(), drop(), toArray() and similar methods, which work lazily, one value at a time. This makes it safe to chain them on an endless generator as long as take() limits the result:
function* idMaker() { let id = 1; while (true) { yield id++; }}
const evens = idMaker() .filter((n) => n % 2 === 0) .map((n) => `order-${n}`) .take(3) .toArray();console.log(evens);This prints:
[ 'order-2', 'order-4', 'order-6' ]Iterator helpers work in current versions of all major browsers and in Node.js 22 and later. See Iterator on MDN for the full list.
Steve Griffith covers this topic in his video ES6 Iterator & Generator Fundamentals.