JS EXPERTChapter 9 · JS Expert
The Iterator Protocol
for...of, spread and array destructuring all work through one agreement, the iterator protocol. An object is iterable when it has a [Symbol.iterator]() method that returns an iterator: an object whose next() returns { value, done }. Arrays, strings, Maps and Sets all follow it, and any class can join in. A generator method, *[Symbol.iterator](), is the shortest way to implement it.
Worked example
How it reads
[Symbol.iterator]()is whatfor...oflooks fornext()returnsdone: trueto say the sequence is over- Destructuring only pulls as many values as it needs

Cloud tip: Implementing the protocol makes your data structures feel native: every tool that loops just works.


