Open In App

Maximum Call Stack Size Exceeded Error

Last Updated : 06 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In JavaScript, we may get an error message that reads “Maximum call stack size exceeded”. This error happens when the call stack – the mechanism used by JavaScript to keep a record of function calls – becomes large and cannot add any more function calls. 

The call stack is a kind of stack data structure that keeps a record of function calls in a program. Whenever a function is called, the data of the call are pushed onto the stack. When the function completes, the data are popped off the stack. This process keeps ongoing as more functions are called and completed.

In some cases, a function can call itself or call another function that leads to calls it back. This forms a loop of function calls, with each call pushing more information to the call stack. If the loop continues for too long or the functions are too deeply nested, the call stack can become too large, and the compiler will throw the “Maximum call stack size exceeded” error.

 

The error message looks like this:

Uncaught RangeError: Maximum call stack size exceeded

Approaches: These are some approaches to be followed to fix the “Maximum call stack size exceeded” error:

  1. Identify the cause:  To resolve the error, identify the function or loop that is causing the call stack to overflow. identify functions that call themselves or call each other in a loop.
  2. Use recursion: By using recursion instead of loops can help to prevent the call stack from becoming large. Recursion is a technique in which a function calls itself. When using recursion, make sure that there is a base case that stops the recursion from going on indefinitely.
  3. Optimize your code: If the code is causing the call stack to overflow, there may need to optimize it to reduce the number of function calls or the depth of the function call stack. identify ways to simplify the code, such as using fewer nested loops or a more efficient algorithm.

Example 1: Recursive function with no base case

Javascript




function reduce(num) {
    console.log(num);
    reduce(num - 1);
}
  
reduce(15);


Output:

RangeError: Maximum call stack size exceeded

The above example goes into infinite recursion because there is no base condition added to stop the recursive calls. There must be some base condition to avoid this.

Example 2: Recursive function with self-calling:

Javascript




function callMyself() {
    console.log('Calling myself!');
    callMyself();
}
  
callMyself();


Output:

RangeError: Maximum call stack size exceeded

The above example goes into infinite recursion because the function is calling itself and as the stack is filled and there will be no more space available for more. This error will throw.

Example 3: Let’s look at an approach to avoid this issue or handle it carefully by adding a base case or termination condition.

Javascript




function reduce(num) {
    if (num <= 0) {
        return;
    }
    console.log(num);
    reduce(num - 1);
}
reduce(15);


Output:

15
14
13
12
11
10
9
8
7
6
5
4
3
2
1

In the above, we added a base condition of termination that when the num value becomes 0 or less than 0, we stop recursive calls.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads