how lexical scoping happening in closures?

153 views Asked by At

var x = 10;
function foo() {
  var y = x + 5;
  return y;
}
function bar() {
  var x = 2;
  return foo();
}


console.log(bar()); // 15

In my view, output should be 7 since foo()'s lexical envt. is bar() and hence value of x should be taken as 2 not 10.

Please correct my understanding

0

There are 0 answers