Sure! Let's dive deep into this:
🔵 What is Execution Context in JavaScript?
An Execution Context (EC) is an abstract concept describing the environment in which JavaScript code is evaluated and executed. Whenever any code is run in JavaScript, it’s run inside an execution context.
It contains information about:
- The code being executed
- The variables and functions defined
- The value of
this
- The scope chain
- Other internal details
In simple terms:
👉 Execution Context = Environment where the code runs.
There are mainly three types of Execution Contexts:
- Global Execution Context (GEC)
- Functional Execution Context (FEC)
- Eval Execution Context (rarely used)
🔵 Concepts Required to Understand Execution Context
Before fully understanding Execution Context, you need to understand these concepts in order:
- Call Stack
- Lexical Environment
- Scope Chain
- Hoisting
- Phases of Execution Context
Let's go through them one by one:
- 📚 Call Stack
- JavaScript is single-threaded, meaning it can do one thing at a time.
- The Call Stack is a data structure that keeps track of function calls.
- When a function is invoked, a new Execution Context is created and pushed onto the Call Stack.
- When the function finishes, its Execution Context is popped off the stack.
Example:
function greet() {
console.log('Hello');
}
function start() {
greet();
}
start();
Call Stack:
- start() is called → start's EC is pushed
- greet() is called inside start → greet's EC is pushed
- greet() finishes → greet's EC is popped
- start() finishes → start's EC is popped
- 📚 Lexical Environment
- A Lexical Environment is a structure that holds identifier-variable mapping.
- It consists of:
- Environment Record (actual storage of variables/functions)
- A reference to the outer (parent) Lexical Environment
In simple words:
👉 Lexical Environment = Local memory + Reference to parent environment.
Example:
function outer() {
let a = 10;
function inner() {
console.log(a);
}
inner();
}
outer();
Here, inner has access to a because of the Lexical Environment.
- 📚 Scope Chain
- When trying to access a variable, JavaScript first looks in the current Lexical Environment.
- If not found, it looks in the outer environment, and so on, forming a chain — called the Scope Chain.
Example:
let a = 10;
function outer() {
let b = 20;
function inner() {
console.log(a, b);
}
inner();
}
outer();
inner can access both a and b because of the Scope Chain.
- 📚 Hoisting
- During the creation phase of an Execution Context, JavaScript moves declarations (variables and functions) to the top of their scope.
- Variables are initialized with
undefined, and functions are fully hoisted.
Example:
console.log(a); // undefined
var a = 5;
foo(); // "Hello"
function foo() {
console.log('Hello');
}
- 📚 Phases of Execution Context
Each Execution Context goes through two phases:
✅ Creation Phase:
- Creation of the Lexical Environment
- Setting up the Scope Chain
- Setting the value of
this
- Hoisting of variables and functions
✅ Execution Phase:
- Code is executed line by line
- Variables are assigned actual values
- Functions are invoked
🔵 Summary Flow:
When JavaScript code runs:
- Global Execution Context is created.
- Code is scanned for variable and function declarations (Creation Phase).
- Variables are hoisted and initialized with
undefined, functions are hoisted with their definitions.
- Code is executed line by line (Execution Phase).
- When a function is called, a new Functional Execution Context is created and pushed onto the Call Stack.
- After function execution, its context is popped off the Call Stack.
🔵 Visual Representation:
Global Execution Context (GEC)
|
|-- Creation Phase
|-- Execution Phase
|
|-- Function call -> New Functional Execution Context (FEC)
|
|-- Creation Phase
|-- Execution Phase
Would you like me to also explain this with a real-world analogy (like a restaurant kitchen example)? 🚀
It can make it even easier to visualize!