BugHunt

RangeError: Maximum call stack size exceeded

A function called itself (directly or through others) with no way to stop. JavaScript's equivalent of infinite recursion.

JavaScriptInfinite loops

What it means

Every call adds a frame to the call stack; the stack has a fixed size, roughly ten thousand frames depending on the engine. Recursion that never reaches its base case fills it. The stack trace repeats the same few function names — those are your loop.

Common causes

1. A recursive function with no reachable base case

The base case exists, but the recursive call never moves towards it.

Breaks

function countdown(n) {
  if (n === 0) return;
  countdown(n);
}

Works

function countdown(n) {
  if (n <= 0) return;
  countdown(n - 1);
}

2. A setter that assigns to itself

this.name = v inside set name() calls the setter again, forever.

Breaks

class User {
  set name(v) { this.name = v.trim(); }
}

Works

class User {
  set name(v) { this._name = v.trim(); }
  get name() { return this._name; }
}

3. Spreading a huge array into a call

Every element becomes an argument; past about a hundred thousand, the call itself overflows.

Breaks

const max = Math.max(...bigArray);

Works

const max = bigArray.reduce((m, x) => (x > m ? x : m), -Infinity);

How to find it in your own code

Open the stack trace and find the repeating pair or trio of function names — that is the cycle. Check the base case is reachable for the input that crashed (negative numbers and non-integers are the usual escapes). For very deep but correct recursion, rewrite it as a loop.

Still not sure why yours breaks?

Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.

Other common errors