Lingua-e
← Errors
JavaScript

Maximum call stack size exceeded: What It Means and How to Fix It

Error reference

Maximum call stack size exceeded

Language

JavaScript

Severity

Critical

When it happens

This RangeError is thrown in JavaScript when the call stack overflows because too many function calls are nested without returning.

Potential fixes

Add a base case to every recursive function and verify that every execution path eventually reaches it. Convert deep recursion to an iterative loop with an explicit stack data structure. Use setTimeout or queueMicrotask to break synchronous call chains and yield control back to the engine.

Deep dive

This RangeError is thrown in JavaScript when the call stack overflows because too many function calls are nested without returning. Each function call adds a frame to the stack, and the engine imposes a limit to prevent uncontrolled memory growth.

Why it happens

  • A recursive function that lacks a base case and calls itself indefinitely.
  • Two functions that call each other in a cycle with no exit condition.
  • An event handler that accidentally re-triggers the same event, causing an infinite loop of calls.
  • Deeply nested promise chains or synchronous callbacks that build up frames.

Potential fixes

Add a base case to every recursive function and verify that every execution path eventually reaches it. Convert deep recursion to an iterative loop with an explicit stack data structure. Use setTimeout or queueMicrotask to break synchronous call chains and yield control back to the engine.

Examples

JavaScript

Code that triggers the error

function countDown(n) {
  console.log(n);
  countDown(n - 1);  // no base case
}

countDown(5);

Error output

5
4
3
2
1
0
-1
...
RangeError: Maximum call stack size exceeded

Fixed code

function countDown(n) {
  if (n < 0) return;   // base case stops recursion
  console.log(n);
  countDown(n - 1);
}

countDown(5);

Practice in English

How would you explain a Maximum call stack size exceeded to a fellow dev? Choose the right phrase:

"I hit an error...

Ready to practice your English at work?

Lingua-e has interactive exercises built around real developer conversations: standups, code reviews, retrospectives, and more. Practice until it comes naturally.

Try Lingua-e for free