Maximum call stack size exceeded error

Asked
Active3 hr before
Viewed126 times

4 Answers

stack
90%

It means that somewhere in your code, you are calling a function which in turn calls another function and so forth, until you hit the call stack limit.,This is almost always because of a recursive function with a base case that isn't being met.,There is a recursive loop somewhere in your code (i.e. a function that eventually calls itself again and again until the stack is full).,In order to fix it, ensure that your recursive function has a base case which is able to be met...

Consider this code...

(function a() {
   a();
})();

In order to fix it, ensure that your recursive function has a base case which is able to be met...

(function a(x) {
   // The following condition 
   // is the base case.
   if (!x) {
      return;
   }
   a(--x);
})(10);
load more v
88%

The most common source for this error is infinite recursion. You must have a recursive function in your code whose base case is not being met and is, therefore, calling the function again and again until you hit the call stack limit.,Use your browser’s console to see which function in your call stack is being repeatedly called. After finding the function, you can easily fix your code by adding a base case.,The following code is an example of infinite recursion, executing it will result in the RangeError:,In older browsers, RangeError also shows up when you try to pass too many arguments to a function that your browser cannot handle.

The following code is an example of infinite recursion, executing it will result in the RangeError:

function foo() {
   foo();
}
foo();
72%

Error Maximum call stack size exceeded  is raised when calling a recursive function in a  loop , This is expected – as every Javascript Engine has a maximum Call Stack Range,JavaScript Closures and Lexical Scoping Using Recursions and Closures Maximum call stack size exceeded Calling function from String Google Dev Tools Tips Ajax and Timeouts Object Reflection Prototypal Inheritance Exception Handling My JavaScript Secrets ,Maximum call stack size exceeded

Failing Javascript Code

function errorFunc(rc) {
   var runCount = rc;
   var maxCount = 100000;
   if ((rc % 5000) === 0) {
      logMessage("errorFunc():: Recursion Count:  " + runCount);
   }
   runCount++;
   if (runCount >= maxCount) {
      logMessage("errorFunc() reached MAX recursion Count :  " + runCount);
      return;
   }
   try {
      errorFunc(runCount);
   } catch (e) {
      if (e instanceof RangeError) {
         logMessage("errorFunc():: " + e + ": " + runCount);
      }
   }
};
load more v
65%

The JavaScript exception "too much recursion" or "Maximum call stack size exceeded" occurs when there are too many function calls, or a function is missing a base case. , In some ways, recursion is analogous to a loop. Both execute the same code multiple times, and both require a condition (to avoid an infinite loop, or rather, infinite recursion in this case). When there are too many function calls, or a function is missing a base case, JavaScript will throw this error. , A function that calls itself is called a recursive function. Once a condition is met, the function stops calling itself. This is called a base case. , This recursive function is missing a base case. As there is no exit condition, the function will call itself infinitely.

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

Other "stack-undefined" queries related to "Maximum call stack size exceeded error"