HardGenerators & Iterators💻 Output Question

next() argument becomes the value of the previous yield

💡

Hint

next(val) injects val as the resolved value of the previous yield. The first next() argument is always ignored.

What does this output?

function* dialog() {
  const name = yield 'What is your name?';
  yield 'Hello, ' + name;
}
const g = dialog();
console.log(g.next().value);
console.log(g.next('Alice').value);

Correct Output

What is your name?
Hello, Alice

Why this output?

Explanation: First next() starts the generator, returns "What is your name?". next("Alice") resumes — "Alice" becomes the result of the yield expression.

Key Insight: next(val) injects val as the resolved value of the previous yield. The first next() argument is always ignored.

More Generators & Iterators Output Questions

MediumGenerator pauses at yield, done true at endMediumreturn in generator — spread ignores return valueMediumyield* delegates to another iterableEasyGenerator as lazy range

Practice predicting output live →

66 output questions with instant feedback

💻 Try Output Quiz