What does the following generator code output? function* gen() { const x = yield 1; yield x + 10; } const g = gen(); console.log(g.next()); // A console.log(g.next(5)); // B

JavaScript Professional Hard

JavaScript Professional — Hard

What does the following generator code output? function* gen() { const x = yield 1; yield x + 10; } const g = gen(); console.log(g.next()); // A console.log(g.next(5)); // B

Key points

  • The first yield in the generator returns the value 1
  • The second yield in the generator returns x + 10
  • The value passed to the second next() call is used as the value of x
  • The correct answer reflects the output based on the generator logic

Ready to go further?

Related questions