Ad Space — Top Banner

Maximum call stack size exceeded

JavaScript Programming Language

Severity: Critical

What Does This Error Mean?

This error means a function kept calling itself (or a chain of functions kept calling each other) with no way to stop. JavaScript can only keep track of a limited number of function calls at once — the 'call stack'. When that limit is hit, JavaScript throws this error to prevent the browser from freezing entirely.

Affected Models

  • All browsers
  • Node.js
  • Deno
  • Any JavaScript runtime

Common Causes

  • A function that calls itself with no stopping condition (infinite recursion)
  • Two functions that call each other back and forth forever (mutual recursion with no exit)
  • An event handler that accidentally triggers itself — like a scroll event that scrolls
  • A recursive algorithm where the base case (stopping condition) is never reached
  • Accidentally passing a function to itself as a callback

How to Fix It

  1. Look at the stack trace in your browser console. It will show the same function name repeating over and over. That repeated function is where the infinite loop is.

    Open console with F12. The stack trace appears below the error message as a list of indented function names.

  2. Find the function that is repeating. Look for the place where it calls itself — that is the recursive call.

    Recursion is when a function calls itself: function countdown(n) { return countdown(n - 1); } — this is infinite because there is no stop condition.

  3. Add a base case — a condition that stops the recursion. For example: if (n <= 0) { return; } at the top of the function.

    Every recursive function needs an exit condition. Without one, it will always eventually hit the call stack limit.

  4. If the recursion should not be happening at all, convert the function to use a loop instead of recursion.

    A while or for loop does the same work without using the call stack, so there is no stack limit to hit.

  5. If an event handler is causing this, check whether the handler is somehow triggering the same event again. Use a flag variable to prevent re-entry.

    Example: let isRunning = false; — set it to true when the handler starts, check it at the top, and set it back to false when done.

When to Call a Professional

This error is always a logic bug in the code. It does not require outside professional help, but it can be tricky to trace in a large codebase. If you cannot find the recursion loop, a rubber-duck debugging session or a Stack Overflow post with the relevant code usually helps.

Frequently Asked Questions

What is a call stack?

The call stack is JavaScript's way of keeping track of which function called which. When function A calls function B, the call stack records that. When function B calls function C, that gets added too. When a function finishes, it gets removed from the stack. If functions keep adding themselves forever without finishing, the stack grows until it overflows.

Can this error crash my browser?

It can freeze or crash the browser tab — usually not the entire browser. Modern browsers detect this and throw the error before the tab becomes completely unresponsive. If your page does freeze, force-close the tab with Ctrl+W or through the browser's task manager.

Is recursion always bad in JavaScript?

No — recursion is a valid and sometimes elegant technique. It is useful for tasks like traversing nested data structures (like a tree or folder structure). The key rule is: every recursive function must have a base case that stops the recursion. For very deep recursion, a loop is safer because it does not use the call stack.