What is the output of the following? “`js function outer() { var x = 1; function inner() { var x = 2; console.log(x); } inner(); console.log(x); } outer(); “`

JavaScript Professional Medium

JavaScript Professional — Medium

What is the output of the following? “`js function outer() { var x = 1; function inner() { var x = 2; console.log(x); } inner(); console.log(x); } outer(); “`

Key points

  • The inner function creates a new variable x that shadows the outer function's x.
  • The console.log inside inner() prints the inner x value of 2.
  • The console.log outside inner() prints the outer x value of 1.

Ready to go further?

Related questions