Sub topic 3
Iterators, next(), and the { value, done } Result
This chapter assumes you know JavaScript functions, objects, and methods, and that you have read the previous chapter on iterables and Symbol.iterator. In that chapter, an iterable was an object that can hand out an iterator. This chapter covers the iterator itself: the object that actually produces values, one at a time.
The iterator protocol
An iterator is any object with a next() method. Each call to next() returns an iterator result, which is a plain object with two properties:
| Property | Meaning |
|---|---|
| `value` | The current item. It is usually `undefined` once iteration is finished. |
| `done` | `false` while there are more items. `true` once the sequence is finished. |
That is the whole contract. No base class or special type is involved, just a method and a result shape.
Stepping through an iterator by hand
Normally for...of calls next() for you. Calling it yourself shows what happens under the hood:
const letters = ['a', 'b'];
const it = letters[Symbol.iterator]();
console.log(it.next()); // { value: 'a', done: false }
console.log(it.next()); // { value: 'b', done: false }
console.log(it.next()); // { value: undefined, done: true }The last item, 'b', comes back with done: false. The iterator only reports done: true on the call after the last item. The consumer then stops and ignores the value that arrived with done: true.
Writing the loop yourself
This while loop does roughly what for...of does:
const it = ['x', 'y', 'z'][Symbol.iterator]();
let result = it.next();
while (!result.done) {
console.log(result.value); // x, y, z
result = it.next();
}Iterators are stateful and one-way
An iterator keeps track of its position, so it can only move forward. When you need to start over, ask the iterable for a new iterator:
const nums = [1, 2];
const a = nums[Symbol.iterator]();
const b = nums[Symbol.iterator]();
console.log(a.next().value); // 1
console.log(a.next().value); // 2
console.log(b.next().value); // 1 (b has its own position)Common errors
- Calling
nexton the iterable. Arrays have nonext()method. Call[Symbol.iterator]()first to get the iterator. - Treating the final
valueas an item. Checkdonebefore you usevalue. - Getting
donewrong. A missingdonecounts asfalse. If your iterator never setsdone: true, every loop over it runs forever. - Reusing a finished iterator and expecting it to start over. It won't.
Iterators can also have optional return() and throw() methods. Later chapters cover them.
Exercise
Given const it = 'hi'[Symbol.iterator](), what do four calls to it.next() return, in order?
Show answer
{ value: 'h', done: false }, then { value: 'i', done: false }, then { value: undefined, done: true }, then { value: undefined, done: true } again, because a finished built-in iterator stays finished.
Exercise
Why does this loop print nothing on its second pass? const it = [1, 2][Symbol.iterator](); for (const n of it) console.log(n); for (const n of it) console.log(n);
Show answer
Both loops use the same iterator object. The first loop runs it until done is true, so the second loop's first next() call already returns done: true. To loop twice, iterate the array itself, which creates a fresh iterator for each loop.