Function Invocation and Variable Environment in JavaScript
This section explains how JavaScript handles function calls and manages variables using execution contexts, illustrated with an example involving two functions, a() and b(), each declaring a variable named x but with different values. We also include a global variable x to demonstrate scope differences.
Understanding this process is fundamental to grasping concepts like scope and closures in JavaScript
Consider the following code:
| index.js | |
|---|---|
Let's break down the execution step-by-step:
Execution Context Overview
When a JavaScript program runs, a Global Execution Context (GEC) is created. Every execution context has two phases:
- Creation Phase: The JavaScript engine sets up the memory space for variables and functions (the Variable Environment). Variables declared with varare initialized withundefined. Function declarations are stored entirely.
- Execution Phase: The code is executed line by line.
The Call Stack manages execution contexts. The GEC is the base of the stack. When a function is called, a new Function Execution Context (FEC) is created and pushed onto the stack. When the function finishes, its FEC is popped off the stack.
Step-by-Step Execution:
- 
GEC Creation: - The GEC is created and pushed onto the Call Stack.
- Memory is allocated for x,a, andbin the GEC's Variable Environment.xis initialized toundefined.aandbstore their function definitions. 
 
- 
GEC Execution (Line 1): - var x = 1;is executed.
- The value 1is assigned to the global variablex. 
 
- 
GEC Execution (Line 2): a()Invocation:- Function a()is called.
- A new Function Execution Context (FEC) for ais created.
- aFEC Creation Phase: Memory is allocated for- a's local variable- x, initialized to- undefined.
- The aFEC is pushed onto the Call Stack. Control enters functiona.
 
- Function 
- 
aFEC Execution (Line 7):- var x = 10;is executed within- a.
- The value 10is assigned to the local variablexwithina's FEC.
 
- 
aFEC Execution (Line 8):- console.log(x);is executed.
- JavaScript looks for xin the current FEC (a's context). It finds the localxwith the value10.
- 10is printed to the console. 
 
- 
aFEC Completion (End of functiona):- Function afinishes execution.
- The FEC for ais popped off the Call Stack and destroyed. Control returns to the GEC at the point whereawas called (after line 2). 
 
- Function 
- 
GEC Execution (Line 3): b()Invocation:- Control is now at line 3 in the GEC.
 
- Function b()is called.
- A new FEC for bis created.
- bFEC Creation Phase: Memory is allocated for- b's local variable- x, initialized to- undefined.
- The bFEC is pushed onto the Call Stack. Control enters functionb.
 
- Control is now at line 3 in the GEC.
- 
bFEC Execution (Line 12):- var x = 100;is executed within- b.
- The value 100is assigned to the local variablexwithinb's FEC.
 
- 
bFEC Execution (Line 13):- console.log(x);is executed.
- JavaScript looks for xin the current FEC (b's context). It finds the localxwith the value100.
- 100is printed to the console. 
 
- 
bFEC Completion (End of functionb):- Function bfinishes execution.
- The FEC for bis popped off the Call Stack and destroyed. Control returns to the GEC at the point wherebwas called (after line 3).   
 
- Function 
- 
GEC Execution (Line 4): - Control is now at line 4 in the GEC.
- console.log(x);is executed.
- JavaScript looks for xin the current execution context (the GEC). It finds the globalxwith the value1.
- 1is printed to the console. 
 
- 
GEC Completion: - The script reaches the end.
- The GEC is popped off the Call Stack. The program finishes.
 
 
This detailed breakdown illustrates how JavaScript manages separate execution contexts for global code and each function call, ensuring variables in different scopes do not interfere with each other.
For video explanation, refer to Namaste Javascript series.