CodeZettaInterview Hub

JavaScript · Execution Context · Junior · Debugging

How does the call stack relate to execution contexts, and what happens on a stack overflow?

Short Interview Answer

Each function call pushes an execution context onto the call stack; returning pops it. Unbounded recursion overflows the stack.

Detailed Explanation

JavaScript is single-threaded for its main call stack: only one execution context runs at a time. Calling a function pushes a new context; returning (or throwing out of the function) pops it. The currently running context is always the top of the stack. A stack overflow (RangeError: Maximum call stack size exceeded) occurs when recursion or mutual calls nest deeper than the engine allows — typically from missing base cases or accidental infinite recursion. Async callbacks do not grow the same synchronous stack indefinitely; they run later as new top-level tasks.

Example

function recurse() {
  return recurse();
}
recurse(); // RangeError: Maximum call stack size exceeded

Interview Tip

Contrast sync stack overflow with async 'infinite' loops that freeze the UI without overflowing.

Common Mistake

Claiming promises or setTimeout prevent all stack issues — deeply nested sync code inside a callback can still overflow.

Did you know this answer?