RangeError

JavaScript ErrorRuntime ErrorMediumLast updated: June 28, 2026Tested on:V8 Engine v11.3ECMAScript 2023June 2026

A RangeError occurs when a value is outside the allowed range for an operation.

RangeError Quick Fix⏱️ Est. Fix Time: 3–15 minutes

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)

Infinite recursion55%
Invalid array length20%
toFixed() precision10%
String.repeat() invalid count8%
Buffer/TypedArray size7%

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

1Add recursion termination: Always verify base cases exist inside recursive functions to halt executions.
2Validate array bounds: Ensure array sizes are positive integers before initialization.
3Limit decimal precision: Keep toFixed() values between 0 and 100, and toPrecision() between 1 and 100.
4Sanitize repeat counts: Validate repeat counts are positive and keep results within memory boundaries.

Framework-Specific Examples

Infinite recursion calls exhaust the V8 call stack size, throwing a RangeError.

Maximum Call Stack Exceeded Example
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.

Still having this problem?

Didn't solve your problem?

SuggestRequest Error