Skip to content
engineering workbench.by StudyPort Academy
All errors
runtime · DEBUG LIBRARY

RangeError: Maximum call stack size exceeded

Locate the failed assumption, reproduce it, then repair the cause.

What actually caused it

A chain of calls grew beyond the engine's available stack. Infinite is a common cause: each call starts another before the current call can return.

A base case is only useful if every repeating path can reach it. Recursing with the same never moves toward termination. A setter that assigns to its own can also call itself indirectly. Inspect repeated names in the stack trace to find the cycle.

Very deep but finite can exceed the stack too. There is no portable safe maximum depth, so unbounded input should not rely on a large number of nested calls.

Minimal reproduction

A recursive call that makes no progress

function countDown(value) {
  if (value === 0) return;
  countDown(value);
}
countDown(3);

Read this reproduction without executing it in your main page; it intentionally exhausts the stack.

The fix

Define the stopping condition, validate the input, and verify that each step makes progress toward that condition. Test zero or empty input as well as the normal case.

For potentially deep traversal, use an explicit loop or work list instead of depending on unlimited call depth. A guard counter can help diagnose a cycle, but a production repair must establish a genuine termination rule.

Move toward a reachable base case

function countDown(value) {
  if (!Number.isInteger(value) || value < 0) {
    throw new Error("Expected a non-negative integer");
  }
  if (value === 0) return;
  console.log(value);
  countDown(value - 1);
}
countDown(3);

This small example terminates. Use a loop for arbitrarily large input.