jsverse
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 and Step both follow your edits
Loading...
Console
Output will appear here
Visualization
Result
Press Step to watch each line's result appear here
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.