Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Generators Advanced in JavaScript

Yield Fibonacci values in JavaScript with function* and a term limit. for...of consumes the generator; create a fresh one to restart an exhausted sequence.

AdvancedAdvanced JavaScriptExample 6 of 12

Run this example once in a fresh browser console. The console shows the printed results.

generators-advanced.js
1function* fibonacci(limit) {
2 let a = 0, b = 1;
3 for (let count = 0; count < limit; count++) {
4 yield a;
5 [a, b] = [b, a + b];
6 }
7}
8const sequence = fibonacci(5);
9for (const value of sequence) console.log(value);
10console.log('Finished:', sequence.next().done);
11console.log('Fresh sequence:', [...fibonacci(3)].join(', '));

Sample Output

0, 1, 1, 2, 3 on separate lines

Five yields happen as for...of requests values.

Finished: true

The iterator has no more values after the finite loop.

Fresh sequence: 0, 1, 1

Calling the generator function again creates new state.

In this one

  • Write function* and yield
  • Produce values one at a time
  • Consume a finite generator
  • Create a fresh sequence after exhaustion

How it works

  1. 1

    Start with a zero and b one.

  2. 2

    Yield a before updating the pair.

  3. 3

    Resume and move a to b and b to the sum.

  4. 4

    Let for...of request five values.

  5. 5

    After exhaustion, done is true; a new three-value generator produces 0, 1, 1.

Edge cases

Spreading an endless generator

An unlimited sequence never finishes building an array.

Use a limit or stop consuming it deliberately. This example makes the number of yields finite.

Large Fibonacci numbers

JavaScript numbers eventually lose exact integer precision.

Keep the lesson small. For large exact terms, use an appropriate BigInt version and do not mix number and BigInt arithmetic.

What's going on

A generator is a function that can pause and later continue from the same place. It uses function* and yield. This example produces Fibonacci numbers, where each new number after the starting pair is the sum of the previous two.

Calling fibonacci(5) creates a generator object; it does not run the whole loop immediately. Each request for the next value resumes the function until yield returns a value and pauses it again. for...of makes those requests for you and stops when the generator is finished.

After five values, sequence is exhausted, so its next result has done true. To start over, call fibonacci again to create a fresh generator. The explicit limit prevents the example from running forever when spread into an array. Generators can describe much larger sequences without first building every value, but JavaScript number precision still limits exact large Fibonacci values.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What is the first yielded number?

Pick an answer to check it.

Question 2

How do you restart an exhausted sequence?

Pick an answer to check it.

Questions people ask

Does calling fibonacci(5) print anything by itself?

No. Its body starts when a value is requested, such as by next or for...of.

Can I loop over sequence again for the same values?

Not after it is exhausted. Call fibonacci(5) again to get a fresh generator object.

What does yield do?

It provides a value to the caller and pauses the generator until the next request.