Iterators and Generators

Advanced · 10 min

The iterator protocol behind `for...of`, how `Symbol.iterator` and `next()` work, and how generators use `yield` to write lazy, even infinite, sequences.

Before thisArrays and Array MethodsClosures

Andrew Burgess · 10 min

Iterators and Generators

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Andrew BurgessReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `for...of`, array destructuring, and spread all work on arrays, strings, `Map`s, and `Set`s for the same underlying reason: each of those types implements the iterator protocol, a shared contract any object can opt into.
  • The protocol has two parts — an object is *iterable* if it has a `[Symbol.iterator]` method that returns an *iterator*, and an iterator is any object with a `next()` method that returns `{ value, done }` on each call.
  • A generator function (`function*`) is a shortcut for writing an iterator by hand: calling it doesn't run the function's body, it returns an iterator, and each `yield` pauses execution and produces the next `{ value, done: false }`.
  • Because a generator only computes the next value when `next()` is actually called, it can represent a sequence that's infinite, or expensive to compute in full — the values that are never asked for are simply never produced.
  • `yield` pauses a generator mid-execution with its local variables and position intact, which is a closure's "remember where you were" behavior playing out one step at a time, rather than a function that runs start to finish in one go.

The question behind for...of

By this point you’ve used for...of on arrays, probably on strings, maybe on a Map or a Set. It’s natural to assume that’s a special case the language hardcodes for those specific built-in types. It isn’t. for...of, array destructuring, and spread all share one underlying mechanism: the iterator protocol, a plain contract that any object — including ones you write yourself — can implement to make itself “loop-able.”

That’s the real payoff of this lesson. Once you understand the protocol, you’re not just learning how for...of works internally; you’re learning how to make your own custom data structures (a linked list, a paginated API client, a tree) work with every piece of syntax that already expects something iterable.

The protocol, in exactly two parts

An object is iterable if it has a method at the special key Symbol.iterator that, when called, returns an iterator. An iterator is any object with a next() method, and calling next() has to return an object shaped like { value: <something>, done: <boolean> } — the next value in the sequence, and whether the sequence is finished.

const range = {
  from: 1,
  to: 3,
  [Symbol.iterator]() {
    let current = this.from;
    const last = this.to;
    return {
      next() {
        return current <= last
          ? { value: current++, done: false }
          : { value: undefined, done: true };
      },
    };
  },
};

for (const n of range) console.log(n); // 1, 2, 3

range isn’t an array or any built-in type — it’s a plain object that happens to implement the protocol, and that’s enough for for...of, spread ([...range]), and array destructuring to all work on it exactly as if it were a native collection. The embedded video covers this same iterator/iterable distinction directly, which is the part worth watching closely if the { value, done } shape doesn’t click immediately from code alone — seeing next() get called step by step makes the contract concrete.

Generators: the protocol without the bookkeeping

Writing the iterator above by hand works, but notice how much of it is bookkeeping — manually tracking current, manually shaping the return object every time. A generator function, written with function* and using the yield keyword, gets the compiler to write that bookkeeping for you:

function* range(from, to) {
  for (let current = from; current <= to; current++) {
    yield current;
  }
}

for (const n of range(1, 3)) console.log(n); // 1, 2, 3

Calling range(1, 3) does not run the function body — it returns an iterator, exactly like the hand-written version above, just automatically. Every time something calls next() on it (directly, or through for...of calling it behind the scenes), the function runs until it hits the next yield, hands back that value wrapped in { value, done: false }, and pauses — with every local variable and its exact position in the loop intact. Calling next() again resumes right where it left off. That pause-and-resume-with-state-intact behavior is closures’ “remember the variables from when I was created” idea, playing out one next() call at a time instead of all at once.

Why “lazy” matters: sequences that never finish

Because a generator only computes a value when it’s actually asked for one, it can represent sequences that would be impossible to build as a real array — including infinite ones:

function* naturalNumbers() {
  let n = 1;
  while (true) {
    yield n++;
  }
}

const numbers = naturalNumbers();
numbers.next().value; // 1
numbers.next().value; // 2
// this could run forever and never run out of memory,
// because nothing is stored beyond "what n currently is"

A function that tried to return all natural numbers as an array would have to finish computing the entire (infinite) array before returning anything, which never happens. The generator sidesteps the problem entirely: it only ever holds its current position, producing the next value on demand and nothing more. This is also why generators show up constantly for things like paginated API results or reading a huge file in chunks — you get a sequence that produces exactly as much as the consumer actually asks for, no more.

Where this fits

This lesson leans on two things you’ve already built up: the array methods lesson, because the iterator protocol is literally what makes for...of and spread work on arrays (and everything else) in the first place; and closures, because a generator’s pause-and-resume behavior is a closure holding onto its local state between calls, just triggered by next() instead of by being called again from outside. From here, this same lazy, pull-one-value-at-a-time model is exactly what the asynchronous JavaScript group builds on next — async/await is, under the hood, generators and promises wired together.

Check yourself

  1. 01What two things does an object need, exactly, to be usable in a `for...of` loop?

    It needs a method at the key `Symbol.iterator` that returns an iterator object, and that iterator object needs a `next()` method which returns `{ value, done }` each time it's called. `for...of` calls `[Symbol.iterator]()` once to get the iterator, then calls `next()` repeatedly until `done` is `true`.

  2. 02You call a generator function, like `const gen = countUp();`. Has any of the function's code run yet? How do you find out what the first value is?

    No — calling a generator function only creates and returns an iterator; none of the function body executes yet. You have to call `gen.next()`, which runs the function up to its first `yield` and returns `{ value: <that yielded value>, done: false }`.

  3. 03Why can a generator represent an infinite sequence (like all natural numbers) without ever running out of memory, when a function that returns an array of the same sequence can't?

    A generator produces one value at a time, only when `next()` is called, and nothing is stored beyond the generator's own paused execution state. A function that builds an array would have to compute and hold every single element in memory before it could return anything — which is impossible once the sequence has no end.

  4. 04What specifically happens to a generator's local variables and its position in the code when it hits a `yield`?

    Execution pauses exactly at that point — the generator doesn't lose its local variables or forget where it was, the way a normal function would if it returned. The next call to `next()` resumes from directly after that `yield`, with every local variable exactly as it was left, which is why generators can maintain state across multiple calls without any external variable to hold it.

  5. 05`for...of` works on a generator's returned iterator directly, without you calling `.next()` yourself. What is `for...of` doing under the hood to make that work?

    It's just running the iterator protocol automatically: call `next()`, check if `done` is `true`; if not, hand `value` to the loop body and call `next()` again, repeating until `done` becomes `true`. A generator already returns something with a working `next()` method, so `for...of` can drive it exactly like it would drive an array's or a `Map`'s iterator.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark