What is the output of the following? “`js let x = 10; const fn = () => { let x = 20; return () => x; }; console.log(fn()()); “`

JavaScript Developer Hard

JavaScript Developer — Hard

What is the output of the following? “`js let x = 10; const fn = () => { let x = 20; return () => x; }; console.log(fn()()); “`

Key points

  • The inner function accesses the x variable declared within its scope.
  • JavaScript functions create closures that allow inner functions to access variables from outer functions.
  • The outer x variable with a value of 10 is not accessible inside the inner function.

Ready to go further?

Related questions