I'm trying to understand how does execution stack work in javascript.
var x = 1;
function f1() {
console.log(x);
}
function f2() {
var x = 2;
f1();
}
f2();
When we declared x as a global variable and those 2 functions, they are stored in global scope object along with 2 function closures. Once we called f2(), a new scope object of f2() created and prepended to global scope object. f1() will be called in f2() so a new scope object of f1() will be prepended to f2()'s scope object. Since f1() doesn't have 'x' and any local variables, it will lookup 'x' in f2()'s scope object and log '2' to the console.
My question is why does console output '1'?
I forget to mention that function calls will push new execution context onto execution stack and points to current scope object