What does the following code output? const arr = [1, 2, 3]; arr[Symbol.iterator] = function* () { yield ‘a’; yield ‘b’; }; console.log([…arr]);

JavaScript Professional Hard

JavaScript Professional — Hard

What does the following code output? const arr = [1, 2, 3]; arr[Symbol.iterator] = function* () { yield ‘a’; yield ‘b’; }; console.log([…arr]);

Key points

  • The Symbol.iterator property is used to define a custom iterator for an object.
  • The spread operator (...) can be used to spread the elements of an iterable object into a new array.
  • The generator function yields values sequentially when iterated over.

Ready to go further?

Related questions