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
Here's a simple JavaScript function that takes a number from an HTML file and throws a RangeError if it's greater than 3000:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Range Error Example</title> </head> <body> <h1>Range Error Example</h1> <p>Enter a number:</p> <input type="number" id="numberInput"> <button onclick="checkNumber()">Submit</button> <script> function checkNumber() { const numberInput = document.getElementById('numberInput'); const number = parseFloat(numberInput.value); if (isNaN(number)) { alert('Please enter a valid number.'); } else if (number > 3000) { throw new RangeError('The number is greater than 3000.'); } else { alert('The number is within the allowed range.'); } } </script> </body> </html>In this example, the JavaScript function
checkNumber()is called when the button is clicked. It retrieves the value from the input field, converts it to a number usingparseFloat(), and then checks if the number is greater than 3000. If it is, a RangeError is thrown with an appropriate error message.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.