RangeError
JavaScript Programming Language
Severity: ModerateWhat Does This Error Mean?
A RangeError means a value is outside the allowed range for a particular operation. For example, creating an array with a negative length, or calling toFixed() with more than 100 decimal places. JavaScript has limits on certain values, and this error means you have gone past those limits.
Affected Models
- All browsers
- Node.js
- Deno
- Any JavaScript runtime
Common Causes
- A function calling itself over and over with no way to stop (infinite recursion)
- Creating an array with an invalid length like new Array(-1) or new Array(Infinity)
- Calling toFixed(), toPrecision(), or toExponential() with a number outside their allowed range
- Passing a value that is too large or too small for the operation being performed
- A loop condition that never becomes false, causing it to run forever
How to Fix It
-
Check the error message for the specific value that is out of range. For example: 'Invalid array length' or 'toFixed() digits argument must be between 0 and 100'.
The message tells you exactly which rule was broken and usually points to the line number.
-
If the error is 'Maximum call stack size exceeded', you have a function calling itself infinitely. Find the function and add a stopping condition.
Every recursive function needs a base case — a condition that says 'stop calling yourself now'.
-
If you are creating an array with a variable length, add a check to make sure the number is positive and not too large before creating it.
Example: if (size > 0 && size < 100000) { let arr = new Array(size); }
-
If you are using toFixed() or toPrecision(), make sure the argument is a whole number between 0 and 100.
Passing a variable to these methods without checking its value first is a common source of RangeErrors.
-
Review any loops or recursive functions and make sure they have a clear exit condition that will eventually be reached.
Trace through the logic manually: if I start with value X, will this loop ever stop?
When to Call a Professional
RangeErrors are developer errors that do not require outside help. The most dangerous form — infinite recursion — can freeze a browser tab. If your page is frozen, close the tab and fix the recursive function before running it again.
Frequently Asked Questions
What is the most common cause of RangeError in JavaScript?
The most common cause is infinite recursion — a function that keeps calling itself with no stopping condition. This quickly uses up all available memory and JavaScript throws a RangeError with the message 'Maximum call stack size exceeded'. This is so common it has its own error page on this site.
Can a RangeError crash my browser?
Yes. An infinite recursion RangeError can cause the browser tab to freeze or become unresponsive. Modern browsers will usually kill the script before it crashes the entire browser. But the tab with the broken page may stop responding until you close it.
Is RangeError the same as an overflow error?
They are similar in concept. In other programming languages, an overflow means a number got too big for its data type. In JavaScript, numbers use floating-point format which handles very large numbers without overflowing in the same way. RangeError is more about invalid arguments to specific functions rather than number overflow.