DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
  1. Home
  2. Articles
  3. Frontend Engineering
  4. JavaScript Execution Context and the Call Stack
XLinkedInReddit
MediumFrontend Engineering

JavaScript Execution Context and the Call Stack

D
DevPrep Team
February 10, 2026·2 min read·0
Table of Contents
  • What is an Execution Context?
  • Types of Execution Contexts
  • Creation Phase vs Execution Phase
  • The Call Stack
  • Stack Overflow
  • Variable Environment
  • This Binding

Understanding execution contexts is fundamental to understanding scope, hoisting, closures, and the this keyword.

What is an Execution Context?

Every time JavaScript runs code, it creates an execution context — an environment that contains all the information needed to execute that code.

Types of Execution Contexts

  1. Global Execution Context: Created when the script first runs. One per program.
  2. Function Execution Context: Created each time a function is called.
  3. Eval Execution Context: Created by eval() (don't use eval).

Creation Phase vs Execution Phase

console.log(x); // undefined (not ReferenceError!)
console.log(y); // ReferenceError: Cannot access before initialization
var x = 5;
let y = 10;

// Creation Phase:
// 1. var x → initialized to undefined (hoisting)
// 2. let y → in Temporal Dead Zone (TDZ)
// 3. Function declarations → fully hoisted

// Execution Phase:
// 1. console.log(x) → undefined
// 2. console.log(y) → ReferenceError (still in TDZ)
// 3. x = 5
// 4. y = 10

The Call Stack

function third() { console.log("third"); }
function second() { third(); }
function first() { second(); }
first();

// Call Stack visualization:
// |           |
// | third()   | ← currently executing
// | second()  |
// | first()   |
// | global    |
// |___________|

Stack Overflow

// Infinite recursion → stack overflow
function infinite() {
  infinite(); // Each call adds to the stack
}
infinite(); // RangeError: Maximum call stack size exceeded

// Fix: use tail recursion or iteration
function factorial(n, acc = 1) {
  if (n <= 1) return acc;
  return factorial(n - 1, n * acc); // Tail position
}

Variable Environment

// Each execution context has its own variable environment
function outer() {
  const a = 1; // outer's variable environment
  
  function inner() {
    const b = 2; // inner's variable environment
    console.log(a + b); // Scope chain: inner → outer → global
  }
  
  inner();
}

This Binding

Each execution context also determines the value of this:

  • Global context: this = window (browser) or global (Node)
  • Function call: this = window (sloppy) or undefined (strict)
  • Method call: this = the object
  • Constructor: this = new object
  • Arrow function: this = enclosing context (lexical)

Related Articles

MediumFrontend Engineering

System Design #12: Design a Multi-Step Form Wizard

7 min read
MediumFrontend Engineering

Mastering Senior-Level JavaScript Interview Concepts

2 min read
MediumFrontend Engineering

System Design #9: Design a Collaborative Text Editor

9 min read

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Table of Contents

  • What is an Execution Context?
  • Types of Execution Contexts
  • Creation Phase vs Execution Phase
  • The Call Stack
  • Stack Overflow
  • Variable Environment
  • This Binding

Series

View all Frontend Engineering articles →

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.