var x = 1;
a(); //10
b(); //100
console.log(x);
function a(){
var x = 10;
console.log(x);
}
function b(){
var x = 100;
console.log(x);
}
- When we run our code, a big box called the Global Execution Context (GEC) is created. It has two parts:
Memory and Code
.
- GEC is pushed into the Call Stack, which keeps track of where we are in our code.
- In the first phase, called the memory phase, variables like x are set to undefined, and functions like a and b have their entire code saved as their value.
- In the second phase, called the execution phase, when a function is called, like a(), a new local Execution Context is created inside the GEC.
- Inside this local Execution Context, a new x variable is created, also set to undefined.
- Then, x is assigned the value 10 and printed in the console log.
- After printing, since there are no more commands to run, the local Execution Context for a() is removed from both the GEC and the Call Stack.
- The cursor goes back to the b() function call, and the same steps repeat.
- The Call Stack keeps track of our progress: [GEC, b()] -> GEC after printing yet another x value as 100 in the console log.
- Finally, when everything is done, the GEC is deleted, and it's removed from the Call Stack. The program ends.
10
100
1