RangeError
In JavaScript, RangeError is the native error type that the engine throws when a value has an acceptable type but falls outside the set or range of values that an operation allows. It extends Error, so it carries a message, a name, and a stack trace. Asking .toFixed() to round a price to more decimal places than it supports throws one:
> const price = 19.99;
undefined
> price.toFixed(101)
Uncaught RangeError: toFixed() digits argument must be between 0 and 100
at Number.toFixed (<anonymous>)
Both price and 101 are numbers, so the types are fine. The digit count is the problem, because .toFixed() accepts only 0 to 100.
When JavaScript Throws RangeError
The engine raises a RangeError while code runs. These are the most common situations:
- Unbounded recursion: A function calls itself until the call stack runs out of room. ECMA-262 doesn’t define this case, and V8 and JavaScriptCore report it as a
RangeError. - Invalid lengths: An array or
ArrayBuffergets a length that’s negative, fractional, or too large, or a string grows past the engine’s maximum length. - Out-of-range arguments: A method gets a digit count, radix, repeat count, or normalization form outside the values it accepts.
- Invalid dates: Code calls
.toISOString()on aDatewhose time value isNaN, or formats one withIntl.DateTimeFormat. The time value is usuallyNaNbecause the string the date came from didn’t parse. - BigInt arithmetic and conversions:
BigInt()gets a number that isn’t an integer, or code divides abigintby0n.
Code also throws a RangeError on purpose to reject an argument that has the right type but the wrong value. The constructor takes the same message and options arguments as Error.
Catching RangeError
Say a price formatter takes the number of decimal places from a user setting. A try/catch block can fall back to two places when the setting is out of range and rethrow every other error:
> function formatPrice(amount, digits) {
… try {
… return amount.toFixed(digits);
… } catch (error) {
… if (error instanceof RangeError) {
… return amount.toFixed(2);
… }
… throw error;
… }
… }
undefined
> formatPrice(19.99, 1)
'20.0'
> formatPrice(19.99, 500)
'19.99'
The instanceof check fails for errors from another realm, such as an iframe or a node:vm context, while a check of error.name still works.
Common Messages
V8, the engine in Chrome and Node.js, attaches these messages to a RangeError most often:
| Message (V8) | Usual Cause | Fix |
|---|---|---|
Maximum call stack size exceeded |
A recursive function with no reachable base case, or recursion deeper than the stack allows | Fix the base case, or rewrite the recursion as a loop |
Invalid array length |
new Array(n) or .length = n with a negative, fractional, or too-large n |
Pass an integer from 0 to 2 ** 32 - 1 |
Invalid time value |
.toISOString() or Intl.DateTimeFormat formatting an invalid date, often from an unparseable string |
Check Number.isNaN(date.getTime()) before formatting |
toFixed() digits argument must be between 0 and 100 |
A digit count outside 0 to 100 passed to .toFixed() |
Clamp the digit count to the allowed range |
Invalid count value: x |
A negative or infinite count passed to .repeat() |
Pass a non-negative finite integer |
The number x cannot be converted to a BigInt because it is not an integer |
BigInt() called with a fractional number, NaN, or Infinity |
Round the number with Math.trunc() first, or pass a string of digits |
Division by zero |
A bigint divided by 0n with / or % |
Check the divisor before dividing |
Invalid string length |
A string built past the engine’s maximum length, often in a runaway loop | Stop the loop that grows the string, or process the data in chunks |
Firefox and Safari word several of these messages differently:
| V8 | Firefox (SpiderMonkey) | Safari (JavaScriptCore) |
|---|---|---|
Maximum call stack size exceeded |
too much recursion, thrown as a non-standard InternalError |
Maximum call stack size exceeded. |
Invalid array length |
invalid array length |
Array length must be a positive integer of safe magnitude. |
Invalid time value |
invalid date |
Invalid Date |
toFixed() digits argument must be between 0 and 100 |
precision x out of range |
toFixed() argument must be between 0 and 100 |
The number x cannot be converted to a BigInt because it is not an integer |
x can't be converted to BigInt because it isn't an integer |
Not an integer |
For the most common row, Maximum call stack size exceeded, the stack trace repeats the name of the recursive function whose base case never runs.
Related Resources
Course
JavaScript Error Handling in Depth for Pythonistas
Port a food bank's all-or-nothing donation import from Python to JavaScript, and learn to define error classes, chain errors with cause, report many errors at once, and use a debugger.
For additional information on related topics, take a look at the following resources:
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Nov. 12, 2026