--- title: RangeError slug: Web/JavaScript/Reference/Global_Objects/RangeError tags: - Error - JavaScript - NeedsTranslation - Object - RangeError - TopicStub translation_of: Web/JavaScript/Reference/Global_Objects/RangeError ---
{{JSRef}}

The RangeError object indicates an error when a value is not in the set or range of allowed values.

Syntax

new RangeError([message[, fileName[, lineNumber]]])

Parameters

message
Optional. Human-readable description of the error
fileName {{non-standard_inline}}
Optional. The name of the file containing the code that caused the exception
lineNumber {{non-standard_inline}}
Optional. The line number of the code that caused the exception

Description

A RangeError is thrown when trying to pass a number as an argument to a function that does not allow a range that includes that number. This can be encountered when attempting to create an array of an illegal length with the {{jsxref("Array")}} constructor, or when passing bad values to the numeric methods {{jsxref("Number.toExponential()")}}, {{jsxref("Number.toFixed()")}} or {{jsxref("Number.toPrecision()")}}.

Properties

{{jsxref("RangeError.prototype")}}
Allows the addition of properties to an RangeError object.

Methods

The global RangeError contains no methods of its own, however, it does inherit some methods through the prototype chain.

RangeError instances

Properties

{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/RangeError/prototype', 'Properties')}}

Methods

{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/RangeError/prototype', 'Methods')}}

Examples

Using RangeError

var check = function(num) {
  if (num < MIN || num > MAX) {
    throw new RangeError('Parameter must be between ' + MIN + ' and ' + MAX);
  }
};

try {
  check(500);
}
catch (e) {
  if (e instanceof RangeError) {
    // Handle range error
  }
}

Specifications

Specification Status Comment
{{SpecName('ES3')}} {{Spec2('ES3')}} Initial definition.
{{SpecName('ES5.1', '#sec-15.11.6.2', 'RangeError')}} {{Spec2('ES5.1')}}  
{{SpecName('ES6', '#sec-native-error-types-used-in-this-standard-rangeerror', 'RangeError')}} {{Spec2('ES6')}}  
{{SpecName('ESDraft', '#sec-native-error-types-used-in-this-standard-rangeerror', 'RangeError')}} {{Spec2('ESDraft')}}  

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Feature Android Chrome for Android Edge Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}

See also