JavaScript Notes
Master JavaScript Execution Context: global, function, eval contexts, call stack, creation vs execution phases, this binding, hoisting, and interview Q&A.
What is an Execution Context?
An Execution Context is the environment JavaScript creates every time it needs to run a piece of code. It contains everything the engine needs: variables, the value of this, a reference to outer scopes, and the current line being executed.
Think of it as a workspace the JavaScript engine sets up before running any code.
One-liner: An execution context is the "room" JavaScript builds to run each piece of code — it has its own variables, this, and knows where to look for outer variables.Types of Execution Context
| Type | When Created | Destroyed When |
|---|---|---|
| Global EC | JavaScript file loads | Program ends |
| Function EC | A function is called | Function returns |
| Eval EC | eval() is called | eval() completes |
Two Phases of Execution Context
Every execution context goes through exactly two phases:
Global Execution Context
The Global EC is created when JavaScript starts. In a browser, this = window. In Node.js, this = global.
var site = "WoHoTech";
let topic = "Execution Context";
function greet() {
return `Welcome to ${site}`;
}
console.log(greet());
console.log(topic);Welcome to WoHoTech Execution Context
Creation Phase (Global EC):
Execution Phase (Global EC):
Line 1: site = "WoHoTech"
Line 2: topic = "Execution Context"
Line 4-6: greet defined (already done in creation)
Line 8: greet() called → new Function EC created
Line 9: console.log(topic)Function Execution Context
Each function call creates its own execution context:
const x = 10;
function add(y) {
const z = 5;
return x + y + z;
}
const result = add(2);
console.log(result);17
Execution Context Stack (Call Stack) Diagram
Nested Function Execution Contexts
function outer() {
const a = 1;
function middle() {
const b = 2;
function inner() {
const c = 3;
return a + b + c; // reads from outer scopes via closure
}
return inner();
}
return middle();
}
console.log(outer());6
Call Stack at deepest point:
inner() can access a and b because its outer references chain up through middle and outer.
What's Inside an Execution Context
Creation Phase — Hoisting Behavior
console.log(a); // undefined (var hoisted, not assigned)
console.log(b); // ReferenceError: TDZ (let)
console.log(fn()); // "hello" — function declaration fully hoisted
var a = 5;
let b = 10;
function fn() {
return "hello";
}undefined ReferenceError: Cannot access 'b' before initialization hello
Creation phase registers:
Execution Context and this
The value of this is determined during the creation phase of the execution context:
const obj = {
name: "WoHoTech",
show() {
console.log(this.name); // this = obj (method call)
},
};
function alone() {
console.log(this); // this = global (or undefined in strict)
}
obj.show();
alone();WoHoTech [global object] (or undefined in strict mode)
Common Mistakes
❌ Mistake 1 — Thinking Variables Are Fully Hoisted
console.log(name); // undefined, NOT "Riya"
var name = "Riya";undefined
var declarations are hoisted but not initialized. Only function declarations are fully hoisted (both declaration + body).
❌ Mistake 2 — Confusing Execution Context with Scope
- Scope = which variables are accessible
- Execution Context = the full environment (variables +
this+ outer reference)
Scope is a property of the execution context's lexical environment. They are related but not the same thing.
❌ Mistake 3 — Not Understanding Call Stack Overflow
function infinite() {
return infinite(); // Each call adds a new EC to the stack
}
infinite(); // Maximum call stack size exceededRangeError: Maximum call stack size exceeded
Each recursive call creates a new execution context on the stack. Infinite recursion never pops any context off, eventually exhausting the stack.
Interview Questions 🎯
Q1. What is an execution context in JavaScript?
An execution context is the environment JavaScript creates to execute a piece of code. It includes: (1) a Variable Environment storing local bindings, (2) an outer environment reference for scope chain lookup, and (3) a this binding. Every function call gets its own execution context, and the global context exists for top-level code.
Q2. What are the two phases of an execution context?
- Creation Phase:
thisis bound, outer reference is set, variable declarations are hoisted (var→undefined,let/const→ TDZ, function declarations → fully stored) - Execution Phase: Code runs line by line, variables get their actual values, function calls create new execution contexts
Q3. What is the Call Stack?
The Call Stack (also called the Execution Context Stack) is a LIFO data structure that tracks all currently active execution contexts. When a function is called, its EC is pushed on top. When it returns, its EC is popped off. The global EC is always at the bottom.
Q4. What is the difference between the Global Execution Context and Function Execution Context?
| Global EC | Function EC | |
|---|---|---|
| - | ----------- | ------------- |
| Created when | Program starts | Function is called |
this | window/global | Depends on call type |
| Destroyed when | Program ends | Function returns |
| Count | One | One per function call |
Q5. How does hoisting relate to execution context?
Hoisting happens during the Creation Phase of execution context. Before any code runs, the engine scans for declarations and: stores function declarations fully, initializes var variables to undefined, and places let/const in the Temporal Dead Zone. That's why you can call function declarations before they appear in code.
Q6. What is the Temporal Dead Zone (TDZ)?
The TDZ is the period between when a let or const variable is registered in the creation phase and when the execution phase actually reaches its declaration. Accessing a variable in TDZ throws a ReferenceError.
Q7. How does JavaScript determine this inside an execution context?
this in an execution context is determined by how the function is called:
- Regular function call → global object (or
undefinedin strict) - Method call → the object before the dot
call/apply/bind→ explicitthisArgnew→ the newly created instance- Arrow function → lexically inherited from enclosing context
Q8. What happens to an execution context after the function returns?
After a function returns, its execution context is popped off the call stack and is eligible for garbage collection — unless a closure holds references to its variables, in which case those variables remain in memory.
Key Takeaways 🔑
- Every time code runs, JavaScript creates an Execution Context with variables,
this, and outer reference - Two phases: Creation (hoisting,
thisbinding) → Execution (code runs) - The Call Stack is a LIFO stack of execution contexts — currently running context is on top
varis hoisted toundefined;let/constare in TDZ; function declarations are fully hoistedthisis determined during the creation phase based on how the function is called- Nested functions chain execution contexts via outer references — this is the scope chain
- Execution contexts are destroyed after function returns (unless captured by closures)
Summary
An execution context is the foundational concept that explains how JavaScript runs. Every time code executes — globally or via a function call — JavaScript creates an execution context with three parts: a variable environment (local bindings), an outer reference (scope chain), and a this binding. It operates in two phases: the Creation Phase (hoisting, this setup) and the Execution Phase (code runs line by line). Multiple execution contexts stack up in the Call Stack, with the active one always on top. Understanding execution contexts explains hoisting, this behavior, scope chains, closures, and call stack overflows — making it the most foundational concept in advanced JavaScript.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for JavaScript Execution Context - Complete Guide with Stack Diagrams.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, advanced, execution, context
Related JavaScript Master Course Topics