# Solve - RangeError - Maximum call stack size exceeded in JS

Tue Oct 19 20212 min read

Photo by Elizeu Dias

## Solve - RangeError - Maximum call stack size exceeded#

The "RangeError: Maximum call stack size exceeded" error occurs when a function is being called so many times that the invocations exceed the call stack limit. To solve the error, specify a base case that has to be met to exit the recursion.

Here's an example of how the error occurs.

index.js
```Copied!```function example() {
// ⛔️ RangeError: Maximum call stack size exceeded
example();
}

example();
``````

We call the function, which then calls itself until the call stack limit is exceeded.

To solve the error, we have to specify a condition at which the function stops calling itself.

index.js
```Copied!```let counter = 0;

function example(num) {
if (num < 0) {
return; // 👈️ this stops the function from endlessly calling itself
}

counter += 1;
example(num - 1);
}

example(4);

console.log(counter); // 👉️ 5
``````

This time we check if the function was invoked with a number that is less than `0` on every invocation.

If the number is less than `0`, we simply return from the function so we don't exceed the call stack's limit.

If the passed in value is not less than zero, we call the function with the passed in value - `1`, which keeps us moving toward the case where the `if` check is satisfied.

A recursive function calls itself until a condition is met. If there is no condition to be met in your function, it will call itself until the maximum call stack size is exceeded.

You might also get this error if you have an infinite loop that calls a function somewhere.

index.js
```Copied!```function sum(a, b) {
return a + b;
}

while (true) {
sum(10, 10);
}
``````
Our `while` loop keeps calling the function and since we don't have a condition that would exit the loop we eventually exceed the call stack size.

This works in a very similar way to a function calling itself without a base condition. The same would be the case if you were using a `for` loop.

Here's an example of how to specify a condition that has to be met to exit the loop.

index.js
```Copied!```function sum(a, b) {
return a + b;
}

let total = 0;

for (let i = 10; i > 0; i--) {
total += sum(5, 5);
}

console.log(total); // 👉️ 100
``````

If the `i` variable is equal to or less than `0`, the condition in the `for` loop is not satisfied, so we exit the loop.

If you can't track exactly where your error occurs, look at the error message in your browser's console or your terminal (if using Node.js).

The screenshot above shows that the error occurred on line `25` in the `index.js` file.

The error might also occur if you're importing the same file more than once, e.g. loading the jQuery script multiple times on the same page.

You can check which scripts are loaded on your page by opening the `Network` tab in your browser's developer tools and refreshing the page.

Use the search field on my Home Page to filter through my more than 1,000 articles.