What is the output of this code? function test() { var x = 1; const inner = () => x; var x = 2; return inner; } console.log(test()());

JavaScript Professional Hard

JavaScript Professional — Hard

What is the output of this code? function test() { var x = 1; const inner = () => x; var x = 2; return inner; } console.log(test()());

Key points

  • Arrow functions maintain a reference to the variables in their lexical scope
  • The value of `x` is 2 when `inner` is called
  • `inner` returns the value of `x` when it was defined, not when it is called

Ready to go further?

Related questions