Open In App
Related Articles

JavaScript InternalError too much recursion

Improve Article
Improve
Save Article
Save
Like Article
Like

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.

Javascript




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.

Javascript




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

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 28 Jun, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials