MediumGenerators & Iterators💻 Output Question

Generator pauses at yield, done true at end

💡

Hint

Generators pause at yield. When the function completes, done becomes true.

What does this output?

function* gen() {
  yield 1;
  yield 2;
}
const g = gen();
console.log(g.next().value);
console.log(g.next().value);
console.log(g.next().done);

Correct Output

1
2
true

Why this output?

Explanation: First next(): value=1, done=false. Second: value=2, done=false. Third: function ends, done=true.

Key Insight: Generators pause at yield. When the function completes, done becomes true.

More Generators & Iterators Output Questions

Mediumreturn in generator — spread ignores return valueMediumyield* delegates to another iterableEasyGenerator as lazy rangeMediumCustom iterable with Symbol.iterator

Practice predicting output live →

66 output questions with instant feedback

💻 Try Output Quiz