RangeError The RangeError object indicates an error when a value is not in the set or…
- User
RangeError The RangeError object indicates an error when a value is not in the set or range of allowed values. Description 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:
- passing a value that is not one of the allowed string values to String.prototype.normalize(), or
- when attempting to create an array of an illegal length with the Array constructor, or
- when passing bad values to the numeric methods Number.prototype.toExponential(), Number.prototype.toFixed() or Number.prototype.toPrecision(). RangeError is a serializable object, so it can be cloned with structuredClone() or copied between Workers using postMessage(). RangeError is a subclass of Error. Constructor Creates a new RangeError object. Instance properties Also inherits instance properties from its parent Error. These properties are defined on RangeError.prototype and shared by all RangeError instances. The constructor function that created the instance object. For RangeError instances, the initial value is the RangeError constructor. Represents the name for the type of error. For RangeError.prototype.name, the initial value is "RangeError". Instance methods Inherits instance methods from its parent Error. Examples Using RangeError (for numeric values)
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 } }Using RangeError (for non-numeric values)
function check(value) { if (!["apple", "banana", "carrot"].includes(value)) { throw new RangeError( 'The argument must be an "apple", "banana", or "carrot".', ); } } try { check("cabbage"); } catch (error) { if (error instanceof RangeError) { // Handle the error } }Specifications Specification ECMAScript Language Specification
sec-native-error-types-used-in-this-standard-rangeerror
Browser compatibility desktop mobile server Chrome Edge Firefox Opera Safari Chrome Android Firefox for Android Opera Android Safari on iOS Samsung Internet WebView Android Deno Node.js RangeError 1 Toggle history 12 Toggle history 1 Toggle history 5 Toggle history 1 Toggle history 18 Toggle history 4 Toggle history 10.1 Toggle history 1 Toggle history 1.0 Toggle history 4.4 Toggle history 1.0 Toggle history 0.10.0 Toggle history RangeError() constructor 1 Toggle history 12 Toggle history 1 Toggle history 5 Toggle history 1 Toggle history 18 Toggle history 4 Toggle history 10.1 Toggle history 1 Toggle history 1.0 Toggle history 4.4 Toggle history 1.0 Toggle history 0.10.0 Toggle history RangeError is serializable 77 Toggle history 79 Toggle history 103 footnote Toggle history 64 Toggle history No Toggle history 77 Toggle history 103 footnote Toggle history 55 Toggle history No Toggle history 12.0 Toggle history 77 Toggle history No Toggle history No Toggle history Legend Tip: you can click/tap on a cell for more information. See also
- Error
- Array
- Number.prototype.toExponential()
- Number.prototype.toFixed()
- Number.prototype.toPrecision()
- String.prototype.normalize() Found a content problem with this page?
- Edit the page on GitHub.
- Report the content issue.
- View the source on GitHub. This page was last modified on Feb 21, 2023 by MDN contributors.
Please give me javascript code to take a number from the html file and throw rangeerror if its greater than 3000
- Assistant
Sure! Here's a JavaScript code snippet that takes a number from an HTML file and throws a RangeError if it's greater than 3000:
HTML file:
<!DOCTYPE html> <html> <head> <title>Range Error Example</title> </head> <body> <input type="number" id="numberInput"> <button onclick="checkNumber()">Check Number</button> <script src="script.js"></script> </body> </html>JavaScript file (script.js):
function checkNumber() { var input = document.getElementById("numberInput").value; if (input > 3000) { throw new RangeError("Number must not be greater than 3000."); } else { // Number is within range, do something else console.log("Number is within range."); } }In this example, the
checkNumber()function is called when the "Check Number" button is clicked. It retrieves the value of the input field with the id "numberInput" and checks if it's greater than 3000. If it is, a RangeError is thrown with the specified message. Otherwise, if the number is within the range, the code inside theelseblock will be executed.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.