What is the output of the following? “`js let result = []; for (var i = 0; i < 3; i++) { result.push(() => i); } console.log(result.map(fn => fn())); “`

JavaScript Developer Hard

JavaScript Developer — Hard

What is the output of the following? “`js let result = []; for (var i = 0; i < 3; i++) { result.push(() => i); } console.log(result.map(fn => fn())); “`

Key points

  • Arrow functions retain the value of variables from their outer scope
  • The map function executes each function in the array and returns a new array with the results
  • The for loop increments i until it reaches 3
  • The output reflects the final value of i in each iteration

Ready to go further?

Related questions