MediumGenerators & Iterators💻 Output Question

return in generator — spread ignores return value

💡

Hint

return in a generator terminates it. Spread/for...of ignores the return value — only yields are collected.

What does this output?

function* gen() {
  yield 1;
  return 99;
  yield 2;
}
const values = [...gen()];
console.log(values.length);
console.log(values.join(','));

Correct Output

1
1

Why this output?

Explanation: Spread stops at done:true. return gives {value:99, done:true} — spread ignores it. yield 2 unreachable.

Key Insight: return in a generator terminates it. Spread/for...of ignores the return value — only yields are collected.

More Generators & Iterators Output Questions

MediumGenerator pauses at yield, done true at endMediumyield* delegates to another iterableEasyGenerator as lazy rangeMediumCustom iterable with Symbol.iterator

Practice predicting output live →

66 output questions with instant feedback

💻 Try Output Quiz