0

So, I'm trying to understand the functional context. But there's one thing I don't understand.

How is the variable myVar not able to assign the value 1 to the function b() which has a value of undefined.

Shouldn't it be able to assign the value since it´s part of the global scope?

If I follow what the execution context says with the creation and then execution. It makes sense that javascript shouldn't assign that value because javascript is synchronous. But I've also been taught that global variables have access to local variables.

function b() {
  var myVar;
  console.log(myVar + "b");
}

function a() {
  var myVar = 2;
  console.log(myVar + "a");
  b();
}

var myVar = 1;
console.log(myVar);
a();
  • It's not the *value* that matters, it's *where* a variable name is declared that matters. The nearest ancestor block with the declared variable name will be what referencing a variable name will refer to. – CertainPerformance Mar 28 '20 at 23:12
  • Please read at the answers for this question. https://stackoverflow.com/q/762011/17447 Welcome to StackOverflow! – naveen Mar 28 '20 at 23:16

0 Answers0