Open In App

JavaScript InternalError too much recursion

The exception of too much recursion or maximum call stack size exceeded occurs when there are many function calls, or even if a function is missing a base case.

Output message:



Error: Out of stack space (Edge)
InternalError: too much recursion (Firefox)
RangeError: Maximum call stack size exceeded (Chrome)

Error Type:

InternalError

A recursive function is a function that calls itself repeatedly. When a condition is met, the function stops calling itself. This is called a base case. If somehow the condition does not meet then the function continues calling itself and the error occurs.



Example 1: This example works perfectly, without errors. Because the stopping condition is when x >= 5.




function recursion(x) {
 
    // Base case
    if (x >= 5)
        return;
    recursion(x + 1); // The recursive call
}
 
function Geeks() {
    try {
        recursion(0);
        console.log("Too much "
            + "recursion error not occurred");
    } catch (e) {
        console.log("Too much "
            + "recursion error occurred");
    }
}
 
Geeks();

Output
Too much recursion error not occurred

Example 2: In this example, the stopping condition is when x >= 1000000000000. This is a large value, so the error occurred.




function recursion(x) {
 
    // Base case
    if (x >= 1000000000000)
        return;
    recursion(x + 1); // The recursive call
}
function Geeks() {
    try {
        recursion(0);
        console.log("Too much recursion"
            + " error not occurred");
    } catch (e) {
        console.log("Too much recursion"
            + " error occurred");
    }
}
 
Geeks();

Output
Too much recursion error occurred

Article Tags :