JavaScript Error

RangeError

RangeError is thrown when a value is not in the expected range. This includes invalid array lengths, exceeding the maximum call stack size, and passing out-of-range numbers to methods.

Common causes

  • Creating an array with invalid length (e.g., negative number)
  • Infinite recursion causing stack overflow
  • toFixed(), toPrecision() with out-of-range arguments
  • Passing invalid values to Number methods
  • String repeat() with negative or infinity count

How to fix it

  • Validate numeric inputs before using them for array sizes
  • Add base cases to recursive functions to prevent infinite recursion
  • Clamp values to valid ranges before passing to methods
  • Use iterative approaches instead of deep recursion
  • Check Number.MAX_SAFE_INTEGER for large number operations

Example

RangeError example
// Error examples
new Array(-1);  // RangeError: Invalid array length

const num = 1.5;
num.toFixed(200);  // RangeError: toFixed() digits argument must be between 0 and 100

// Infinite recursion
function recurse() { recurse(); }
recurse();  // RangeError: Maximum call stack size exceeded

// Fix: validate array length
const len = Math.max(0, userInput);
new Array(len);

// Fix: add base case
function factorial(n) {
  if (n <= 1) return 1;
  return n * factorial(n - 1);
}

Track RangeError with Checkend

Checkend automatically captures RangeError errors in your JavaScript application with full context:

  • Complete stack trace with source maps
  • Browser and environment context
  • Automatic grouping of similar errors
  • Instant notifications when errors occur

Stop debugging in production

Get full error context and fix issues faster with self-hosted error tracking.