So,I am trying to grasp the concept of scope chain in javascript and therefore I created the following example to check if I got it right.
Note: I am familiar with the following concepts(Execution Contexts and Lexical Environments).
Example:
function test(){
function test2(){
//This prints 2 as expected
console.log(a);
}
//This should print 1 but instead it prints undefined
console.log(a);
var a = 2;
test2();
}
var a = 1;
test();
if for example I comment the following:
//var a = 2;
Then the output in both cases is 1.