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.
Examples
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
How to fix it
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.
Fixed code
function countDown(n) {
if (n < 0) return; // base case stops recursion
console.log(n);
countDown(n - 1);
}
countDown(5);Related errors
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