RangeError
An out-of-range value error.
Available since: ES3
Syntax
new RangeError(message)Parameters
| Parameter | Type | Description |
|---|---|---|
| message | string | Optional. A description of the error. |
Return value
RangeErrorAn error object.
Examples
Built-in methods throw RangeError:
try {
(3.14).toFixed(200);
} catch (error) {
console.log(error.name);
}
try {
new Array(-1);
} catch (error) {
console.log(error.name);
}Output:
RangeError
RangeErrorYour own RangeError:
function setVolume(level) {
if (level < 0 || level > 100) throw new RangeError('Volume must be 0–100');
return level;
}
try {
setVolume(150);
} catch (error) {
console.log(error.message);
}Output:
Volume must be 0–100Notes
Infinite recursion also leads to a RangeError with the message Maximum call stack size exceeded.
See also
In the tutorial
- Errors and try...catch — Error handling in JavaScript: try, catch and finally blocks, the error object, the throw statement and built-in error types.