Iterators & generators
Understand the idea
A custom collection stores values correctly, but for...of says it is not . Storage alone does not define how should proceed.
A cursor through results
Some work should happen only when another value is requested. You might read a sequence in small pieces or stop as soon as you find what you need. Iterators let the consumer ask for one more result without requiring the producer to build an entire first.
The next result has two parts
An iterable supplies a at Symbol.iterator that returns an iterator. Arrays and strings already implement this protocol, which is why for...of can consume them. An ordinary object is not iterable just because it has ; use an appropriate collection of its entries when that is what you need.
Calling next returns an object with value and done. A value of alone does not mean the sequence is finished: undefined can be a legitimate yielded value. The done flag is what tells you whether the iterator has completed.
| Term or operation | Meaning | A useful distinction |
|---|---|---|
| Iterator | Has a next() operation returning value and done | Represents one progression through results |
| Iterable | Provides an iterator through Symbol.iterator | Can be consumed by for...of or spread |
| Generator function (function*) | Creates a generator object | Its body runs as the consumer requests results |
| yield | Offers one value and pauses | Does not finish the generator |
| return in a generator | Finishes it with a final result | done is true; for...of does not yield that final value |
Iterable versus iterator
A generator function, written with function*, creates an iterator when called. Its body does not run to completion immediately. A call to next starts or resumes it until yield pauses and supplies a value. A return finishes it with done set to true.
A generator pauses at yield
Generators make lazy sequences readable. Values are produced when requested rather than all stored upfront. This can reduce intermediate allocation, but it does not make expensive computation . Work between yields still runs on the current thread.
Do not assume a cursor restarts
An iterator may be exhausted after one traversal. A generator function can create another fresh iterator, while reusing the finished iterator produces no more values. Make infinite sequences explicit and give consumers a stopping rule; spreading an infinite iterator into an array never finishes.
Calling a generator function creates a generator object; it does not immediately execute the whole body. The first next starts it, each yield pauses it, and the next request resumes after that yield. Once this generator finishes, further next calls stay done. Call the generator function again when you want a fresh independent cursor.
Mental model
FOLLOW THE VALUE
No yielded values have been requested yet.
01 / 03Trace the transformation
Live example
Two next calls receive ticket values. The third confirms completion instead of producing a third ticket.
Your console output appears here.
Guided challenge
Yield the second value
Keep the generator open long enough to yield 102 as a normal iteration value.
Your console output appears here.
EXPECTED CONSOLE OUTPUT
101 102 false true
Hints 0/2
Use all hints or make 3 failed runs (0/3).
Free sandbox
Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.
Your console output appears here.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 200 XP. You can always revisit it.