EasyGenerators & Iterators💻 Output Question

Generator as lazy range

💡

Hint

Generators produce values lazily — perfect for ranges, sequences, and infinite streams.

What does this output?

function* range(start, end) {
  for (let i = start; i <= end; i++) yield i;
}
const sum = [...range(1, 5)].reduce((a, b) => a + b, 0);
console.log(sum);

Correct Output

15

Why this output?

Explanation: range(1,5) yields 1,2,3,4,5. Spread collects them. reduce sums: 15.

Key Insight: Generators produce values lazily — perfect for ranges, sequences, and infinite streams.

More Generators & Iterators Output Questions

MediumGenerator pauses at yield, done true at endMediumreturn in generator — spread ignores return valueMediumyield* delegates to another iterableMediumCustom iterable with Symbol.iterator

Practice predicting output live →

66 output questions with instant feedback

💻 Try Output Quiz