Advanced JavaScript / Generators
Generators
A function that can pause mid-execution with yield, and resume exactly where it left off.
A generator function (`function*`) doesn't run to completion when called - it returns an iterator instead. Each call to `.next()` runs the generator until the next `yield`, hands back that value, and freezes the function's entire state - local variables, position, everything - until `.next()` is called again.
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?
Calling numbers() doesn't execute the body at all - it just creates a paused generator. Each next() resumes execution up to the following yield, then pauses again.