An execution context is the actual runtime object the engine creates every time code runs, it's what makes scope, hoisting, and `this` real instead of abstract rules. The call stack is just a stack of these.
1. How many execution contexts exist at the moment a deeply nested function call is executing?
Interview Questions
2 questions
Cheatsheet
Download-ready reference
Cheatsheet
Execution Context = the engine's runtime record for a running piece of code.
Contains: Variable Environment (var, function decls)
Lexical Environment (let/const + parent scope reference)
`this` binding (resolved at creation, based on call site)
Global Execution Context: created once, always the bottom of the call stack.
Function Execution Context: created fresh on EVERY call, never reused.
Call stack = LIFO stack of execution contexts.
call โ push new context
return โ pop context off
Each context repeats: creation phase (hoist + set up bindings) โ execution phase (run code).
"Popped off the stack" โ "destroyed", a closure's reference keeps the
environment record alive on the heap even after its context is popped.
Stack overflow = unbounded pushes without pops (usually broken recursion).
The beginner framing: every time a function runs, JavaScript sets up a little workspace for it, a place to hold its local variables, know what this refers to, and remember where to return to. That workspace is the execution context.
The precise mental model: an Execution Context is the concrete, spec-defined object the engine creates for every piece of running code, there's exactly one Global Execution Context (created once, when your script starts) and a new Function Execution Context for every single function call. Each context bundles three things:
Variable Environment, where var declarations and function declarations live.
Lexical Environment, where let/const live, plus the reference to the parent scope (this is what makes the scope chain from Scope actually work, it's not a separate mechanism, it's a field on the execution context).
this binding, resolved once, when the context is created, based on how the function was called (see this Binding).
The engine-level view: the call stack is a stack (LIFO) of execution contexts. Calling a function pushes a new context on top; returning from it pops that context off. This isn't a metaphor, it's literally what Chrome DevTools shows you in the "Call Stack" panel during a paused debugger session, frame for frame.
function third() { console.log("in third"); }function second() { third(); }function first() { second(); }first();// Stack grows: [Global] โ [Global, first] โ [Global, first, second] โ [Global, first, second, third]// Then unwinds back down to [Global] as each function returns.
The call stack, one execution context at a time
step 1 / 6
function third() {
console.log("in third");
}
ย
function second() {
third();
}
ย
function first() {
second();
}
ย
first();
Global
always the bottom frame
(empty)
Before anything runs, the Global Execution Context is created and pushed onto the call stack.
Every execution context, not just the global one, goes through the same two phases described in Hoisting: a creation phase (set up the variable/lexical environment, determine this, hoist declarations) followed by an execution phase (run the code line by line). This is why hoisting "resets" for every function call, each call gets its own brand-new execution context with its own creation phase.
function counter() { console.log(count); // undefined, fresh creation phase, fresh hoisting, every call var count = 1; return count;}counter(); // undefined logged, returns 1counter(); // undefined logged again, NOT 2, because this is a new context entirely
function recurse() { return recurse();}recurse(); // RangeError: Maximum call stack size exceeded
Every call pushes a new context. The stack has a finite size (engine- and platform-dependent, typically a few thousand to tens of thousands of frames). Recursion with no base case pushes forever until the stack's memory limit is hit, the error name is a direct description of the mechanism, not a metaphor.
Notice validate and double never coexist on the stack, validate fully returns before double is ever called, because JavaScript is single-threaded and function calls are strictly sequential within a single execution context's execution phase.
This is a simplified, userland approximation of what the engine does automatically and invisibly on every call, which is exactly why "read the call stack" is the first move in debugging any "who called this and in what order" question.
Every render of a component is (indirectly) a function call, and React's reconciler is itself driving a call stack as it renders your component tree depth-first, <Parent> renders, which calls <Child>, which calls <Grandchild>, exactly like the nested function calls above. This is also the root reason why an uncaught error in a deeply nested component can crash the whole tree: an unhandled exception unwinds the JavaScript call stack just like it would in any other code, and without an Error Boundary to catch it at some ancestor context, that unwinding propagates all the way up.
React 18+'s concurrent rendering complicates this picture (the "stack" becomes interruptible via Fiber, which is really a linked-list reimplementation of the call stack that can pause and resume), but the mental model of "each render is a context, contexts nest, nesting can unwind on error" still holds.
1. Assuming variables persist between separate calls#
function addItem() { var items = items || []; items.push("thing"); return items;}addItem(); // ["thing"]addItem(); // ["thing"] again, NOT ["thing", "thing"]
Each call gets a fresh execution context, so items is re-declared (and re-initialized to undefined, then []) every time. To persist state across calls, you need a closure over a variable in an outer, longer-lived scope, not a local variable re-declared every call.
2. Misreading a stack trace as call order instead of a stack#
A stack trace prints innermost-first, the function where the error actually happened at the top, its caller below it, and so on down to where execution started. Reading it top-to-bottom as "what happened first" is backwards; the bottom of the trace is what happened first.
3. Not realizing async code creates a NEW context later, not "pauses" the current one#
function fetchData() { console.log("before"); // context A setTimeout(() => { console.log("callback"); // a DIFFERENT, later execution context, A is long gone }, 0); console.log("after"); // still context A}
The setTimeout callback doesn't run inside fetchData's execution context, fetchData's context is created, runs to completion (logging "before" then "after"), and is popped off the stack long before the callback's own, brand-new context is ever created. See Event Loop for the full mechanism.
Keep call stacks shallow where you can, very deep synchronous call chains make debugging harder and (rarely, but really) risk stack overflow in recursive code without proper base cases or tail-call elimination (which most engines don't reliably provide, despite it being in the ES6 spec).
Use the DevTools call stack panel, not just console.log, to debug "how did we get here" questions, it's a direct, live view of the exact structure described in this topic.
Convert deep, unbounded recursion to iteration when the input size isn't guaranteed small, recursion depth is bounded by stack size, not by your algorithm's correctness.
Creating an execution context has real (if small) overhead, it's part of why extremely hot inner-loop code sometimes benefits from being written iteratively rather than via many small recursive function calls, though modern JIT compilers optimize this well in most cases.
Stack traces are expensive to generate, code that constructs new Error().stack (or throws/catches) in a hot path pays a real cost, because the engine has to walk and serialize the current context chain. Avoid doing this in tight loops purely for logging.
Tail-call optimization (reusing the current context instead of pushing a new one for a tail-position recursive call) is in the ES6 spec but only reliably implemented in JavaScriptCore (Safari), don't rely on it for deep recursion in code that needs to run everywhere.