"RangeError" in JavaScript — What It Means and How to Fix It
What the JavaScript error "RangeError" means and how to fix it.
Published October 29, 2026
RangeError is thrown when a numeric value is outside the allowed range, such as negative array lengths or toFixed(200).
What it means and why it happens
Typical causes: creating arrays with invalid sizes, repeat() with negative counts, or exceeding recursion limits.
Read the full message: it names the file and line where the problem was detected, which is the best place to start.
What you see
run the code
RangeError: Invalid array length
Practical examples
Code that triggers it
new Array(-1)The fix
const n = Math.max(0, size)
const a = new Array(n)Common sources
'x'.repeat(-1) // RangeError
(1.5).toFixed(101) // RangeErrorCommon mistakes
- Not validating computed sizes.
- Unbounded recursion (Maximum call stack size exceeded).
- Passing huge values to toFixed/toPrecision.
Key takeaways
- RangeError: RangeError is thrown when a numeric value is outside the allowed range, such as negative array lengths or toFixed(200).
- Read the message and the line number first.
- Fix the cause, don't just silence the error.
FAQ
Is stack overflow a RangeError?
Yes, 'Maximum call stack size exceeded' is a RangeError.
How do I find which line caused RangeError?
The error message and stack trace show the file and line; start there and work backwards.