The RangeError
object indicates an error when a value is not in the set or range of allowed values.
A RangeError
is thrown when trying to pass a value as an argument to a function that does not allow a range that includes the value.
This can be encountered when:
String.prototype.normalize()
, orArray
constructor, orNumber.prototype.toExponential()
, Number.prototype.toFixed()
or Number.prototype.toPrecision()
.RangeError()
RangeError
object.RangeError.prototype.message
RangeError
should provide its own message
property, in SpiderMonkey, it inherits Error.prototype.message
.RangeError.prototype.name
Error
.RangeError.prototype.fileName
Error
.RangeError.prototype.lineNumber
Error
.RangeError.prototype.columnNumber
Error
.RangeError.prototype.stack
Error
.function check(n) { if( !(n >= -500 && n <= 500) ) { throw new RangeError("The argument must be between -500 and 500.") } } try { check(2000) } catch(error) { if (error instanceof RangeError) { // Handle the error } }
function check(value) { if(["apple", "banana", "carrot"].includes(value) === false) { throw new RangeError('The argument must be an "apple", "banana", or "carrot".') } } try { check("cabbage") } catch(error) { if(error instanceof RangeError) { // Handle the error } }
Desktop | ||||||
---|---|---|---|---|---|---|
RangeError |
1 | 12 | 1 | 5.5 | 5 | 1 |
RangeError() constructor |
1 | 12 | 1 | 5.5 | 5 | 1 |
Mobile | ||||||
---|---|---|---|---|---|---|
RangeError |
1 | 18 | 4 | 10.1 | 1 | 1.0 |
RangeError() constructor |
1 | 18 | 4 | 10.1 | 1 | 1.0 |
Server | |
---|---|
RangeError |
0.1.100 |
RangeError() constructor |
0.1.100 |
Error
Array
Number.toExponential()
Number.toFixed()
Number.toPrecision()
String.prototype.normalize()
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RangeError