Advanced JavaScript / Iterators
Iterators
The protocol behind for...of — an object with a next() that hands back values one at a time.
An iterator is any object with a `next()` method that returns `{ value, done }`. Arrays, strings, Maps, and Sets all expose `Symbol.iterator`, a method that produces an iterator over their contents — that's what `for...of` actually calls behind the scenes, once per loop pass, until `done` is true.
Codeeditable — Run executes your edits
Loading...
Console
Output will appear here
Visualization
State
Step through — the console panel shows what's happening
Why?
The iterator keeps its own internal position between calls. Once it has handed back every element, next() reports done: true and value: undefined forever after.