12.1-Execution-Context
6.1 Execution Context
Introduction
Every time JavaScript runs code, it creates an execution contextβan environment where the code is evaluated and executed. Understanding execution contexts is key to understanding scope, hoisting, this, and closures.
What is an Execution Context?
An execution context is a container that holds information about the environment in which code is currently running.
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β EXECUTION CONTEXT β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β VARIABLE ENVIRONMENT β β
β β β’ Variable declarations β β
β β β’ Function declarations β β
β β β’ Arguments object β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β LEXICAL ENVIRONMENT β β
β β β’ let/const declarations β β
β β β’ Outer environment reference β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β THIS BINDING β β
β β β’ What 'this' refers to β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Types of Execution Contexts
1. Global Execution Context (GEC)
Created when JavaScript first runs your code.
// Global Execution Context
var globalVar = "I'm global";
let globalLet = 'Also global';
function greet() {
console.log('Hello!');
}
// Everything here is in the Global Execution Context
Characteristics:
- β’Only ONE global context per program
- β’Creates the global object (
windowin browsers,globalin Node.js) - β’Sets
thisto the global object - β’Created before any code executes
2. Function Execution Context (FEC)
Created every time a function is called (not declared).
function outer() {
// New Function Execution Context created
var outerVar = 'outer';
function inner() {
// Another new Function Execution Context
var innerVar = 'inner';
}
inner(); // FEC for inner() created here
}
outer(); // FEC for outer() created here
3. Eval Execution Context
Created when code runs inside eval(). (Rarely used, avoid in practice)
eval('var x = 10;'); // Creates its own execution context
Execution Context Lifecycle
Every execution context goes through two phases:
Phase 1: Creation Phase
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β CREATION PHASE β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β 1. Create Variable Object (VO) β
β βββ Scan for function declarations β
β βββ Scan for variable declarations (var) β
β βββ Set up arguments object β
β β
β 2. Create Scope Chain β
β βββ Current VO + all parent VOs β
β β
β 3. Determine 'this' binding β
β β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Phase 2: Execution Phase
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β EXECUTION PHASE β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β 1. Assign values to variables β
β 2. Execute function calls β
β 3. Execute code line by line β
β β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Visualizing the Phases
function greet(name) {
var greeting = 'Hello';
var message = greeting + ', ' + name + '!';
return message;
}
greet('Alice');
Creation Phase for greet("Alice"):
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β FUNCTION EXECUTION CONTEXT - greet("Alice") β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Creation Phase: β
β β
β arguments: { 0: "Alice", length: 1 } β
β name: "Alice" β
β greeting: undefined β hoisted, not yet assigned β
β message: undefined β hoisted, not yet assigned β
β this: global object (or undefined in strict mode) β
β β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Execution Phase:
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Execution Phase (line by line): β
β β
β Line 2: greeting = "Hello" β
β Line 3: message = "Hello, Alice!" β
β Line 4: return "Hello, Alice!" β
β β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
The Global Execution Context in Detail
In Browsers:
// Before any code runs, JavaScript creates:
// Global Object
window = {
// Browser APIs
console: {...},
document: {...},
setTimeout: function() {...},
// ... many more
};
// this = window in global scope
console.log(this === window); // true
// Global variables become properties of window
var name = "Alice";
console.log(window.name); // "Alice"
// let and const do NOT become window properties
let age = 25;
console.log(window.age); // undefined
In Node.js:
// Global object is 'global', not 'window'
console.log(global);
// But 'this' at module level is NOT global
console.log(this === global); // false
console.log(this); // {} (module.exports)
// var declarations are NOT added to global
var name = 'Alice';
console.log(global.name); // undefined
Multiple Execution Contexts
var name = 'Global';
function first() {
var name = 'First';
second();
console.log(name); // "First"
}
function second() {
var name = 'Second';
console.log(name); // "Second"
}
first();
console.log(name); // "Global"
Execution Flow:
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β 1. Global Execution Context Created β
β βββ name = "Global" β
β βββ first = function β
β βββ second = function β
β β
β 2. first() called β FEC for first() created β
β βββ name = "First" β
β βββ Calls second() β
β β
β 3. second() called β FEC for second() created β
β βββ name = "Second" β
β βββ Logs "Second" β
β βββ second() FEC destroyed β
β β
β 4. Back to first() β
β βββ Logs "First" β
β βββ first() FEC destroyed β
β β
β 5. Back to Global β
β βββ Logs "Global" β
β β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Variable Environment vs Lexical Environment
var (Variable Environment)
function example() {
console.log(x); // undefined (hoisted)
var x = 10;
console.log(x); // 10
}
let/const (Lexical Environment)
function example() {
// console.log(y); // ReferenceError: Cannot access before initialization
let y = 20;
console.log(y); // 20
}
The Temporal Dead Zone (TDZ):
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β function example() { β
β // TDZ for 'y' starts here β
β // β
β // console.log(y); β ReferenceError β
β // β
β let y = 20; // TDZ ends here β
β console.log(y); // 20 β
β } β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Scope Chain in Execution Context
Each execution context has a reference to its outer (parent) environment:
var a = 10;
function outer() {
var b = 20;
function inner() {
var c = 30;
console.log(a + b + c); // 60
}
inner();
}
outer();
Scope Chain:
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β SCOPE CHAIN β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β inner() EC β
β βββ c = 30 β
β βββ Outer Reference β outer() EC β
β β β
β βΌ β
β outer() EC β
β βββ b = 20 β
β βββ inner = function β
β βββ Outer Reference β Global EC β
β β β
β βΌ β
β Global EC β
β βββ a = 10 β
β βββ outer = function β
β βββ Outer Reference β null β
β β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
this Binding in Execution Context
this is determined when the execution context is created:
// Global context: this = global object
console.log(this); // window (browser) or global (Node)
const obj = {
name: 'Object',
method: function () {
// Method context: this = obj
console.log(this.name); // "Object"
},
arrow: () => {
// Arrow function: this = lexical (outer) this
console.log(this); // window/global
},
};
obj.method(); // "Object"
obj.arrow(); // window/global
Summary
| Concept | Description |
|---|---|
| Execution Context | Environment where code runs |
| Global EC | Created first, one per program |
| Function EC | Created on each function call |
| Creation Phase | Sets up variables, scope, this |
| Execution Phase | Runs code line by line |
| Variable Environment | Holds var declarations |
| Lexical Environment | Holds let/const, scope chain |
| Scope Chain | Links to outer environments |
Next Steps
In the next section, we'll explore the Call Stackβhow JavaScript keeps track of all these execution contexts.