RangeError
A RangeError occurs when a value is outside the allowed range for an operation.
Usually happens because:
- ☑ Infinite recursion (Stack overflow)
- ☑ Array size is negative or too large
- ☑ Invalid precision in toFixed()
🔍 Quick Checklist:
📊 Where this usually happens (estimated occurrence)
What is RangeError?
A RangeError is thrown by the JavaScript runtime when a numeric argument or value exceeds the boundaries of allowed values for a given operation. Unlike a TypeError, where the data type itself is incorrect (e.g. attempting to call a string as a function), a RangeError indicates that the type is correct (typically a number), but the value falls outside the logical constraints of the method or object constructor.
Common Causes
- Maximum call stack exceeded: Executing infinite recursion without a termination condition.
- Invalid array length: Creating a new Array constructor with a negative size or exceeding maximum safe memory lengths.
- Invalid number precision: Setting precision bounds on toFixed() or toPrecision() outside the supported ranges.
- Invalid string repeat: Calling repeat() with negative counts or values that cause the string to exceed maximum length limits.
- Invalid TypedArray size: Initializing binary typed arrays (like Uint8Array) with negative counts.
- Invalid time component values: Supplying values that fail range checks in date-formatting APIs.
Common Mistakes
- Omitting recursive stopping conditions.
- Passing user input size variables directly to new Array() or Uint8Array() without sanitizing boundaries.
- Providing excessively large precision values to toFixed() or toPrecision().
How to Fix
Framework-Specific Examples
Infinite recursion calls exhaust the V8 call stack size, throwing a RangeError.
function hello() {
hello();
}
try {
hello();
} catch (err) {
console.error(err.message); // Maximum call stack size exceeded
}Best Practices
- Always validate numeric variables boundaries before passing them to native methods.
- Prefer iteration (like standard loops) instead of recursion when handling deep tree traversals.
- Catch RangeError in input parsing stages to handle invalid sizes gracefully.
Frequently Asked Questions (FAQ)
Q: What causes a RangeError?
A RangeError occurs when a value is outside the acceptable range for a JavaScript operation or API.
Q: Is Maximum Call Stack Size Exceeded a RangeError?
Yes. In most JavaScript engines, exceeding the call stack limit results in a RangeError.
Q: What's the difference between RangeError and TypeError?
RangeError: The value's type is correct, but its value is outside the valid range. TypeError: The value is of the wrong type or is being used in an invalid way.
Q: Can RangeError happen in Node.js?
Yes. Node.js APIs such as Buffer, streams, and other core modules can throw RangeError when given invalid values.
Q: Can TypeScript prevent RangeError?
TypeScript can catch some mistakes during development, but it cannot prevent runtime values from being outside the valid range.