Search lands in PR-5.1 (Pagefind).

Explanation Beginner

Chapter 2 Updated

How JS is Executed & Call Stack

The two phases of execution, and the LIFO stack that keeps track of where JS is.

  • Full 12m
  • Revision 3m
  • Flow 2m

The two phases

Memory Creation → Code Execution.

  • Phase 1 scans the whole program — variables get undefined, function declarations get their entire body.
  • Phase 2 runs line by line — values assigned, functions invoked, contexts pushed.
  • Nothing in Phase 2 is executed during Phase 1; the split is strict.
  • Hoisting is the side-effect of this split, not a feature in its own right.

Every call = new context

Functions never share their Execution Context.

  • A function invocation creates a brand new Execution Context with its own Memory + Code.
  • Parameters + locals live inside that context’s Variable Environment.
  • When return fires the context is destroyed and popped.
  • Two calls to the same function run in two separate contexts — no shared state.

The Call Stack

LIFO stack of Execution Contexts.

  • Last In, First Out — the most recent call is always on top.
  • GEC is pushed first, sits at the bottom for the whole program.
  • Every invocation pushes; every return pops.
  • Stack is empty → program is done.

The many names

Interviewers use all of these.

  • Call Stack — the common name.
  • Execution Context Stack — what it literally holds.
  • Program Stack / Control Stack — classical terms.
  • Runtime Stack / Machine Stack — engine-level terms.
  • All of them mean the same thing: the LIFO stack of contexts.

Return + overflow

Two edges of stack discipline.

  • return; without a value → function returns undefined.
  • No return statement at all → also returns undefined.
  • Infinite recursion without a base case → Stack Overflow.
  • Error: RangeError: Maximum call stack size exceeded (V8 allows ~10–15K frames).

The Call Stack has no timer

And no concurrency.

  • Whatever is pushed runs immediately — no “wait a second”.
  • setTimeout callbacks don’t sit on the Call Stack; they wait in the callback queue.
  • Because JS is single-threaded, blocking the stack freezes the whole app.
  • This is why async I/O pushes work out to Web APIs and re-enters via the event loop.

Comments

Comments are disabled in this environment. Set PUBLIC_GISCUS_REPO, PUBLIC_GISCUS_REPO_ID, and PUBLIC_GISCUS_CATEGORY_ID to enable.