Sub topic 1
Iterables and Iterators: A Quick Refresher
Iterator helpers are methods that work on iterators. Before using them, you need a clear picture of what an iterator is and how it differs from an iterable. This chapter reviews the iteration protocol that every later chapter builds on.
Prerequisites
This chapter assumes you are comfortable with:
- Modern JavaScript syntax: arrow functions,
const/let, and spread (...) - Array methods such as
mapandfilter for...ofloops- Generator functions (
function*) andyield
Two Roles: Iterable and Iterator
The iteration protocol defines two separate roles.
| Role | What it must have | Examples |
|---|---|---|
| Iterable | A `[Symbol.iterator]()` method that returns an iterator | Arrays, strings, `Map`, `Set`, generator objects |
| Iterator | A `next()` method that returns `{ value, done }` | The result of `array.values()`, generator objects |
An iterable is something you can loop over. An iterator is the object that does the looping, one step at a time. Each call to next() returns a result object. When done is true, the sequence is finished.
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 }What for...of Actually Does
A for...of loop runs the protocol for you. It calls [Symbol.iterator]() once, then calls next() repeatedly until done is true. Spread syntax and destructuring follow the same steps.
Writing an Iterable by Hand
You can implement the protocol yourself. This object is iterable because it has [Symbol.iterator], and that method returns a fresh iterator each time.
const countdown = {
from: 3,
[Symbol.iterator]() {
let current = this.from;
return {
next() {
return current > 0
? { value: current--, done: false }
: { value: undefined, done: true };
},
};
},
};
console.log([...countdown]); // [3, 2, 1]
console.log([...countdown]); // [3, 2, 1]The spread works twice because each spread asks the iterable for a new iterator.
Generators Play Both Roles
A generator object is both an iterator and an iterable. Its [Symbol.iterator]() returns the generator object itself. Generators are the easiest way to produce iterators, and they appear in most examples in this tutorial.
function* pair() {
yield 1;
yield 2;
}
const g = pair();
console.log(g[Symbol.iterator]() === g); // true
console.log(g.next().value); // 1
console.log([...g]); // [2]The spread got only [2] because g had already produced 1. An iterator keeps its position. It does not restart.
Exercise
Given const it = [10, 20, 30].values(); it.next();, what does console.log([...it]) print, and why?
Show answer
It prints [20, 30]. The call to it.next() already consumed 10. Spreading an iterator continues from its current position, and because it[Symbol.iterator]() returns it itself, the spread does not restart the sequence.
Exercise
Is a plain object { next() { return { value: 1, done: true }; } } usable in a for...of loop? Explain.
Show answer
No. It is iterator-like because it has next(), but it has no [Symbol.iterator] method, so it is not iterable. for...of throws a TypeError saying the object is not iterable.