Featured post
- Get link
- X
- Other Apps
RangeError: Maximum call stack size exceeded in Javascript fixed with examples
Hi friends,in this article we are discussing about a RangeError type in javascript we will cover the below points one by one
- What is RangeError: Maximum call stack size exceeded and why it comes in recursive function
- How to fix RangeError: Maximum call stack size exceeded using a termination condition in the recursive function
What is RangeError: Maximum call stack size exceeded and why it comes in recursive function
Let us write a recursive function to get sum of all positive integers less than a given number including that number
function foo(num) {
return num+ foo(--num);
}
let num=10
let totalSum= foo(num);
console.log(`Total sum of all positive integers less than ${num} is ${totalSum}`)
when we run the above lines of code we will get the error as given below
return num+ foo(--num);
^
RangeError: Maximum call stack size exceeded
at foo (C:\Users\Administrator\Desktop\JS\callstackerror.js:2:5)
at foo (C:\Users\Administrator\Desktop\JS\callstackerror.js:2:17)
at foo (C:\Users\Administrator\Desktop\JS\callstackerror.js:2:17)
at foo (C:\Users\Administrator\Desktop\JS\callstackerror.js:2:17)
at foo (C:\Users\Administrator\Desktop\JS\callstackerror.js:2:17)
at foo (C:\Users\Administrator\Desktop\JS\callstackerror.js:2:17)
at foo (C:\Users\Administrator\Desktop\JS\callstackerror.js:2:17)
at foo (C:\Users\Administrator\Desktop\JS\callstackerror.js:2:17)
at foo (C:\Users\Administrator\Desktop\JS\callstackerror.js:2:17)
at foo (C:\Users\Administrator\Desktop\JS\callstackerror.js:2:17)
Node.js v18.14.0
When we examine the above code,we can understand that foo() method is called recursively without any termination condition.So call stack size is exceeded with foo() methods call and it throws the RangeError: Maximum call stack size exceeded.
How to fix RangeError: Maximum call stack size exceeded using a termination condition in the recursive function
So now let us look into to the same example and fix the Maximum call stack size exceeded by adding a termination condition.Our requirement is to get the sum of all positive integers below a number including the number also.I solved the RangeError as shown below.
function foo(num) {
if(num<1) return 0;
return num+ foo(--num);
}
let num=10
let totalSum= foo(num);
console.log(`Total sum of all positive integers less than ${num} is ${totalSum}`)
we will get the result as shown below
Total sum of all positive integers less than 10 is 55
I hope you understood the reason behind RangeError: Maximum call stack size exceeded error and How to avoid the RangeError
Popular Posts
How to Check React Version Installed in Your System by Checking PACKAGE.JSON File and Executing Command from COMMAND PROMPT
- Get link
- X
- Other Apps
Execution failed for task ':react-native-reanimated:externalNativeBuildDebug' solved
- Get link
- X
- Other Apps
Error: Invalid `prisma.$executeRaw()` invocation: fixed using log: ["query"] option
- Get link
- X
- Other Apps
ERROR Warning: React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined
- Get link
- X
- Other Apps
app keeps stopping error solved in android studio emulator using logcat in react native project
- Get link
- X
- Other Apps