Javascript community.
Anthony Alicea's course called "Understanding The Weird Parts" (ECMAScript 5), Section 2, video 16: The Scope Chain, I think I found an inconsistency in how the JS interpreter executes functions using the scope chain.
I understand that everything in Javascript happens in its own execution context, the global is created, then each function is created executed, placed on the stack, taken off the stack, etc.. I get this.
However, I am not understanding the scope chain when a function is placed onto the execution stack at the same level as the global context, why it would not scope outside and to the global context for a variable's value and instead console.log it as undefined.
Please look at the following code as an example:
//Following function is created within the global execution context
function a3() {
console.log(myVar3); //The console.log generates a 'undefined' here, why not a 1?
function b3(){
console.log(myVar3); //console.log generates a 2, because it follows the scope chain
//and looks outside of its execution context; why does function a3
//not do this with the global context?
}
var myVar3 = 2;
b3();
}
var myVar3 = 1; // Global execution context
a3(); // Global execution context
Why doesn't the console.log within function a3, not look to the global execution context and print a 1 for the value? It seems inconsistent with the same pattern for the function call within a3 to b3. myVar3 within the b3 function goes outside its execution context and finds the value of 2.
Please can someone explain why the inconsistent treatment?
I am trying to get a good, base understanding before learning the lastest ECMAScript's features. Please only offer comments for ECMAScript 5 back when the 'var' keyword was used and not the latest 'let' or 'const' constructs.
Thank you for any and all replies!