Understanding Execution Context in JavaScript
When executing code in JavaScript, there are two crucial components:
- Thread of Execution – the process of going through the code line by line and executing it.
- Memory – a storage space for variables, constants, and functions during execution.
What Happens When a Function Runs?
A function in JavaScript operates like a mini-program, requiring both execution flow and a memory space. These two elements together form what is called an execution context.
When a function executes, JavaScript creates a new execution context that consists of:
- Thread of Execution – moves through the function’s code line by line.
- Local memory – stores any variables, constants, and functions declared within the function.
The Global Execution Context
As soon as JavaScript begins executing a file, it creates a global execution context. This is the primary execution environment where the entire script runs.
When a function is invoked, JavaScript creates a new execution context for that function. This temporary execution context exists only while the function runs and is destroyed afterward.
Example Walkthrough
Consider the following function:
function multiplyByTwo(num) {
const result = num * 2;
return result;
}
const output = multiplyByTwo(3);
const newOutput = multiplyByTwo(10);
Step-by-Step Execution:
- Global Execution Context is created.
- It initializes memory and sets up the script.
multiplyByTwo(3)is called.- A new execution context is created.
- Local memory is allocated.
numis assigned the value3(argument passed in).resultis calculated as3 * 2 = 6.6is returned and assigned tooutput.- The execution context is destroyed.
multiplyByTwo(10)is called.- Another execution context is created.
- Local memory is allocated.
numis assigned the value10.resultis calculated as10 * 2 = 20.20is returned and assigned tonewOutput.- The execution context is destroyed.
Execution Flow and Context Stack
Since JavaScript is single-threaded, only one execution context runs at a time. The Call Stack (or Execution Stack) manages this by pushing new execution contexts when functions are invoked and popping them when functions return values.
Understanding Execution Context Phases
Every execution context goes through two main phases:
- Creation Phase:
- The function’s execution context is created.
- Memory is allocated for variables and functions.
- Variables are initialized with
undefined(hoisting happens here).
- Execution Phase:
- Code runs line by line.
- Variable values are assigned, and functions are executed.
Key Takeaways
- Execution context consists of execution flow and local memory.
- Global execution context runs the entire script.
- Function execution contexts are created temporarily when a function is invoked.
- JavaScript follows a single-threaded execution model, handling one operation at a time.
- The Call Stack helps manage execution by keeping track of function calls.
- The execution context has Creation and Execution phases, crucial for understanding hoisting.
Understanding execution context is fundamental to mastering JavaScript, especially when dealing with function calls, closures, and asynchronous programming.
