Sobes.tech
Middle+

let a = 2 function foo() { console.log(a) } function bar() { let a = 1 foo() } bar() //

sobes.tech AI

Answer from AI

In this code, calling bar() will output 2.

Explanation:

The function foo outputs the value of the variable a, which is declared outside the functions and equals 2. Inside bar, a local a = 1 is declared, but foo does not use the local a from bar because foo is closed over the lexical environment where a equals 2.

That is, foo always looks at a from the outer scope, where it is 2, regardless of where foo is called.

Example:

let a = 2;

function foo() {
  console.log(a); // always 2
}

function bar() {
  let a = 1;
  foo(); // will output 2
}

bar();