Skip to content

Latest commit

 

History

History
114 lines (91 loc) · 3.25 KB

File metadata and controls

114 lines (91 loc) · 3.25 KB
title slug tags browser-compat
RangeError
Web/JavaScript/Reference/Global_Objects/RangeError
Class
JavaScript
Object
RangeError
javascript.builtins.RangeError

{{JSRef}}

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 {{jsxref("String.prototype.normalize()")}}, or
  • 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.prototype.toExponential()")}}, {{jsxref("Number.prototype.toFixed()")}} or {{jsxref("Number.prototype.toPrecision()")}}.

RangeError is a {{Glossary("serializable object")}}, so it can be cloned with {{domxref("structuredClone()")}} or copied between Workers using {{domxref("Worker.postMessage()", "postMessage()")}}.

Constructor

  • {{jsxref("RangeError/RangeError", "RangeError()")}}
    • : Creates a new RangeError object.

Instance properties

  • {{jsxref("Error.prototype.message", "RangeError.prototype.message")}}
    • : Error message. Inherited from {{jsxref("Error")}}.
  • {{jsxref("Error.prototype.name", "RangeError.prototype.name")}}
    • : Error name. Inherited from {{jsxref("Error")}}.
  • {{jsxref("Error.prototype.cause", "RangeError.prototype.cause")}}
    • : Error cause. Inherited from {{jsxref("Error")}}.
  • {{jsxref("Error.prototype.fileName", "RangeError.prototype.fileName")}} {{non-standard_inline}}
    • : Path to file that raised this error. Inherited from {{jsxref("Error")}}.
  • {{jsxref("Error.prototype.lineNumber", "RangeError.prototype.lineNumber")}} {{non-standard_inline}}
    • : Line number in file that raised this error. Inherited from {{jsxref("Error")}}.
  • {{jsxref("Error.prototype.columnNumber", "RangeError.prototype.columnNumber")}} {{non-standard_inline}}
    • : Column number in line that raised this error. Inherited from {{jsxref("Error")}}.
  • {{jsxref("Error.prototype.stack", "RangeError.prototype.stack")}} {{non-standard_inline}}
    • : Stack trace. Inherited from {{jsxref("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) === 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
    }
}

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}

See also

  • {{jsxref("Error")}}
  • {{jsxref("Array")}}
  • {{jsxref("Number.toExponential()")}}
  • {{jsxref("Number.toFixed()")}}
  • {{jsxref("Number.toPrecision()")}}
  • {{jsxref("String.prototype.normalize()")}}