Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JS Puzzles · Const

Recursion limits

JavaScript engines cap the number of nested recursive calls because each call uses stack space. The exact limit varies by engine, and exceeding it can raise a maximum-stack-size error.

How it works

Each recursive call creates another execution context on the call stack, so deeper recursion consumes more memory. JavaScript engines impose a runtime-dependent maximum depth; exceeding it typically raises a stack-overflow error, making iteration preferable for very deep work.

Syntax

function name(parameters) { /* recursive call */ }

Key points

  • Recursion depth counts every active nested invocation, including the first function call.
  • For a recursive process whose deepest active chain contains n calls, the recursion depth is n.
  • The limit is imposed by the runtime engine rather than defined as one universal number for all JavaScript environments.
  • Active execution contexts consume memory, so increasing recursion depth increases the memory needed by the call stack.
  • Going beyond the available stack can result in a maximum-stack-size error.
  • Some engines can optimize a final recursive call so the current context need not remain on the stack.

Common mistakes

  • There is no single portable recursion limit: the maximum depth depends on the JavaScript engine.
  • Even a tail-position recursive call is not a safe way to assume unlimited depth, because most engines do not provide the relevant optimization.

Example: Observing the runtime limit

function recurse(depth) {
  try {
    recurse(depth + 1);
  } catch (error) {
    return depth;
  }
}

console.log(recurse(0));

This deliberately recurses until the runtime rejects another nested call; the reported depth varies by JavaScript engine, so it is for experimentation rather than production logic.

When to use it

Measure an environment's practical limit

Testing runtime behavior across browsers or JavaScript hosts.

Prefer an explicit stack when depth may be large

Processing deeply nested trees or graphs.

Set and validate a safe maximum depth

Designing recursive algorithms with bounded input.

Interview questions

QWhat is recursion depth, and why is it limited?

Recursion depth is the maximum number of nested calls, counting the initial call. Each call adds an execution context to the stack, and the engine limits how many contexts can coexist.

Follow-up: What determines the exact limit? The JavaScript engine and its available stack configuration; code should not depend on a particular numeric threshold.

QWhy can deep recursion fail even when the algorithm is logically correct?

A recursive call creates another execution context, which consumes memory while callers wait to resume. Consequently, sufficiently deep recursion can exceed the stack and produce a maximum-stack-size error.

Follow-up: How would you handle very deep input? Rewrite the algorithm iteratively, often using an explicit stack or another loop-based worklist.

QDoes tail recursion remove JavaScript's recursion limit?

Tail-call optimization can avoid retaining the caller's execution context when the recursive call is the final operation. The documentation notes that support is not universal, so it cannot generally be relied on in JavaScript.

Follow-up: Does putting the recursive call last guarantee safety? No; the running engine must implement the optimization, and most engines do not.

  • recursion depth
  • execution context
  • stack
  • tail call optimization
  • maximum stack size exceeded

Source: Recursion and stack. Checked 2026-10-15.