What are the call stack and execution context in JavaScript?

IntermediateJavaScript interview questionBy , updated 17 November 2026

Short answer

An execution context is the record the engine creates to run a piece of code: the global code, or one call of a function. It holds that code's variables, its scope chain and its this. The call stack is the list of active contexts. Calling a function pushes a new context on top, returning pops it, and the engine always runs the context on top.

What is inside an execution context

There are three kinds: the global context (created once when a script starts), one function context per function call, and a context for code run by eval. ES modules get their own module context. Each one holds:

  • Variable bindings. Parameters, var, let, const, class and function declarations for that code.
  • A link to the outer scope. This is where the scope chain comes from. It points to where the function was written, not to the caller.
  • The this value. Set by how the function was called, except in arrow functions, which have no this of their own.
  • Where it is up to. Which line is running, so the engine can resume after a nested call returns.

The specification describes this with terms like LexicalEnvironment and environment records. You do not need those words in an interview, but you should know a context is created per call, not per function definition. Call the same function twice and you get two separate contexts with separate variables.

Creation phase and execution phase

A common way to explain a context is in two steps. The spec does not use these names, but the model matches what engines do.

  1. Creation. Before any line runs, the engine scans the code and creates every binding. var names are set to undefined. Function declarations are set to the whole function. let, const and class names are created but left uninitialised. For a function context, parameters get their arguments and this is decided.
  2. Execution. The code runs top to bottom. Assignments fill in values, and let and const become usable when their declaration line runs.

Hoisting falls straight out of this. A function declaration can be called above its line because it already exists after creation. A var reads as undefined early because creation gave it that value. A let throws because creation left it uninitialised, which is the temporal dead zone.

Watching the stack

function multiply(a, b) {
  return a * b;
}

function square(n) {
  return multiply(n, n);
}

function printSquare(n) {
  console.log(square(n));
}

printSquare(4);

The stack changes like this, with the top on the right:

  1. global
  2. global → printSquare
  3. global → printSquare → square
  4. global → printSquare → square → multiply
  5. multiply returns 16 and is popped, then square returns and is popped
  6. global → printSquare → console.log, which prints 16, then everything unwinds back to global

Each entry is a stack frame. It is last in, first out: the most recent call must finish before the one below it can continue. JavaScript runs on one thread, so there is one call stack, and while it is busy nothing else on that thread can run. That is why a long loop freezes the page. Callbacks from timers, promises and events wait in queues until the stack is empty; the event loop guide covers that hand-off.

Stack overflow

The stack has a size limit. Recursion that never stops, or goes too deep, fills it.

function countDown(n) {
  return countDown(n - 1); // no base case
}

countDown(10);
// Chrome and Node: RangeError: Maximum call stack size exceeded
// Firefox: InternalError: too much recursion

The maximum depth is not fixed by the language. It depends on the engine, its settings and how big each frame is, so a recursive function that works on 5,000 items might fail on 50,000. Ways to fix it:

  • Add or fix the base case. Most overflows are a missing or unreachable stop condition.
  • Rewrite as a loop. For a tree or graph, keep your own array as a stack instead of using the call stack.
  • Use a trampoline. The function returns a thunk for the next step and a loop calls it, so the stack never grows.

The spec includes proper tail calls, which would let some recursion run without growing the stack, but only Safari implements them. Do not rely on them.

Reading a stack trace

When an error is created, the engine records the current call stack in error.stack.

function a() { b(); }
function b() { c(); }
function c() { throw new Error("boom"); }
a();

In Chrome or Node this prints roughly:

Error: boom
    at c (app.js:3:22)
    at b (app.js:2:16)
    at a (app.js:1:16)
    at app.js:4:1

Read it from the top. The first frame is where the error was created, with file, line and column. Each line below is the function that called the one above it, down to the code that started the chain. Usually the bug is in the top frame that belongs to your code, not inside a library. Firefox and Safari use a different format (c@app.js:3:22), but the order is the same.

Some practical points:

  • In production the code is minified, so frames point at one long line. Source maps translate them back to your original files.
  • V8 keeps only the top 10 frames by default. Error.stackTraceLimit changes that in V8-based engines.
  • console.trace() prints the current stack without throwing.
  • After an await, the original callers have already returned. V8 adds at async frames to show the chain of awaiting functions, but frames from before a setTimeout callback are lost.

Practise it

Avoid stack overflow in deep recursion with a trampoline is the direct exercise. Predict hoisting and TDZ results tests the creation phase, and Predict JavaScript error types covers errors unwinding the stack. Group frontend errors by fingerprint uses parsed stack frames, and Predict the event-loop order shows what runs once the stack is empty.

Common follow-up questions

What happens to the stack when an error is thrown?

The engine unwinds it: it pops frames one by one, looking for a try block with a catch or finally. If it finds one, execution continues there. If it reaches the bottom, the error is reported as uncaught. Any finally blocks on the way run as their frames are left.

What happens to an async function's context at await?

Its context is suspended and taken off the stack, so other code can run. When the awaited promise settles, a microtask puts the context back on the stack and it resumes from the same point, with its variables intact. Generators work the same way at each yield.

Is the call stack the same as the memory stack?

Not exactly. The call stack is the language-level list of active calls. Engines store frames in native stack memory, but objects, arrays and closures that outlive a call live on the heap, and the garbage collector frees them later.

How is the execution context different from scope?

Scope is static and comes from where code is written. An execution context is created at runtime for each call and refers to a scope. Two calls of one function share the same scope shape but have different contexts and different variable values.

Mistakes to avoid

  • Saying the engine moves code to the top

    Bindings are created early; the code stays where it is.

  • Assuming one context per function

    There is one per call.

  • Writing recursive code for input of unknown size without a depth limit or an iterative version

  • Reading a stack trace from the bottom and blaming the first library frame you see

  • Debugging minified production traces without uploading source maps

Practise it in code

Interviewers rarely stop at the definition. These questions open in an editor with tests.

  1. Avoid stack overflow in deep recursion with a trampolineEasy15 min
  2. Predict hoisting and TDZ results for functions, var, let, const and classEasy15 minFree
  3. Predict JavaScript error types and what try/catch/finally returnsEasy20 min
  4. Group frontend errors by fingerprint: normalise stack frames across deploys, then count and sample each groupMedium40 min
  5. Predict the event-loop order of setTimeout, promises, queueMicrotask and awaitEasy15 minFree