Skip to content

RangeError: Maximum call stack size exceeded

Error message

                Maximum call stack size exceeded
              

Quick summary

Your code has infinite or excessive recursion — a function calls itself without a proper base case, filling up the call stack until it overflows.

Why this happens

  • A recursive function has no base case (stopping condition)

  • The base case is never reached due to a logic error

  • Two functions call each other in an infinite cycle

Minimal example

✗ Broken code
function recurse(n) {
  return recurse(n + 1);
}
recurse(0);
✓ Fixed code
function recurse(n) {
  if (n >= 10) return n;
  return recurse(n + 1);
}
recurse(0);

The broken function has no base case — it calls itself forever. The fix adds a stopping condition when n >= 10.

How to diagnose

  • Check if the recursive function has a base case that will definitely be reached

  • Trace the function calls to see if the arguments are progressing toward the base case

  • Look for mutual recursion where functions call each other in a cycle

How to fix

  • Add or fix the base case so the recursion stops

  • Ensure the recursive call moves toward the base case (e.g., decrement a counter)

  • Convert deep recursion to an iterative loop if the depth is very large

How to prevent

  • Always include and test the base case in recursive functions

  • Prefer iteration over recursion for very deep or unbounded data

Related Resources

Related Glossary

Related Lessons

← Back to language