What does the following code produce? function* gen() { yield 1; yield 2; return 3; } const g = gen(); console.log([…g]);

JavaScript Developer Medium

JavaScript Developer — Medium

What does the following code produce? function* gen() { yield 1; yield 2; return 3; } const g = gen(); console.log([…g]);

Key points

  • The spread operator (...) is used to convert the generator into an array
  • Only yielded values are included in the array, not the returned value
  • The generator function stops at the return statement
  • The correct answer is [1, 2], as it includes the yielded values

Ready to go further?

Related questions