Skip to content
Concept 36 // Module 04: Functions & Execution Model

Execution Context & Execution Phases

Module 04: Functions & Execution Model•Concept 36 of 100• 5 min read

Execution Context & Execution Phases

The **Execution Context** is an abstract environment created by the engine whenever code is evaluated and executed. Every line of code runs inside one of three types: **Global**, **Function**, or **Eval** contexts.

The Core Concept

Syntax Reference
// 1. Global Context
// Created when the script starts. 
// "this" = window (in browser)

// 2. Function Context
function demo() {
  // A new context is created EVERY time 
  // this function is called.
}

demo(); // Context created
demo(); // Another new context created

Live Interactive Studio

Stack trace: 1) Global Context pushed. 2) `step1` pushed. 3) `step2` pushed. 4) `step2` popped. 5) `step1` popped. 6) Global remains.
JavaScript Editor
Loading...
Console Output
Waiting for output...

Key Code Patterns in Action

01

The Call Stack

// 1. The Call Stack
function step1() {
  console.log("In step 1");
  step2();
  console.log("Leaving step 1");
}

function step2() {
  console.log("In step 2");
}

step1();
Stack trace: 1) Global Context pushed. 2) `step1` pushed. 3) `step2` pushed. 4) `step2` popped. 5) `step1` popped. 6) Global remains.
02

Creation vs Execution Phases

// 2. Creation vs Execution Phases
console.log(name); // undefined (Hoisted)
// console.log(age); // ReferenceError (TDZ)

var name = "Alex";
let age = 25;
During the **Creation Phase**, the engine allocates memory. `var` is set to `undefined`, while `let` is kept uninitialized.
03

The "this" keyword

// 3. The "this" keyword
console.log(this); // window (Global context)

const user = {
  name: "Subhajit",
  sayHi() {
    console.log(this.name); // Subhajit (Object context)
  }
};

user.sayHi();
The value of `this` depends on which execution context is currently active. Inside `sayHi`, the context is the `user` object.
04

1. Core Usage: Execution Context & Execution Phases

// Core syntax and usage for Execution Context & Execution Phases
const feature = 'execution-context-execution-phases';
console.log('Concept 36:', feature);
console.log('Active:', true);
Illustrates the fundamental invocation and syntax structure for Execution Context & Execution Phases.
05

2. Defensive Handling & Edge Case

// Edge-case defensive handling
function handleSafe(input) {
  if (input === null || input === undefined) return 'safe-fallback';
  return String(input).trim();
}
console.log(handleSafe('execution-context-execution-phases'));
console.log(handleSafe(null));
Shows how to write defensive, safe JavaScript code that handles nullish or unexpected inputs when working with Execution Context & Execution Phases.
06

3. Clean Architecture & Production Pattern

// Production-grade pattern for Execution Context & Execution Phases
const settings = Object.freeze({
  id: 'execution-context-execution-phases',
  ready: true,
  timestamp: Date.now()
});
console.log('Settings initialized:', settings.id, settings.ready);
Demonstrates clean, immutable production code avoiding unintended state mutations.

Real-World Engineering Takeaways

Debugging: Understanding stack traces in the browser console.
Performance: Avoiding deep nesting which creates many Stack Frames.
Hoisting: Knowing why some variables are available early and others aren't.
Recursion: Managing how many contexts you push to avoid Stack Overflows.

Under the Hood: Engine & Memory Mechanics

How JavaScript Executes This
### **The Engine's Workflow** **1. Creation Phase (Memory Allocation):** - **Variable Environment:** Memory is allocated for variables and functions. - **Outer Reference:** The link to the Scope Chain is built. - **'this' Binding:** The value of the `this` keyword is determined. **2. Execution Phase (Line-by-line):** - The **CPU** executes the instructions. - Values are assigned to variables. - Function calls trigger new contexts on the **Stack**. **3. Hardware Context:** - **The Call Stack:** Lives in high-speed memory area of the **RAM**. - **Context Size:** Each context takes a small bit of memory. Browsers limit the stack depth (usually ~10,000 calls) to protect the **CPU** and system stability.

Quick Knowledge Check

Test your understanding on these 10 questions (one by one):

Score: 0 / 10
Question 1 of 10 • Core Purpose & Mental Model

What is the core purpose of Execution Context & Execution Phases in JavaScript?

// Concept 36: Execution Context & Execution Phases
// Understanding the core mental model
1 / 10