What is a range error in javascript?

The RangeError() constructor creates an error when a value is not in the set or range of allowed values.

Syntax

new RangeError()
new RangeError(message)
new RangeError(message, options)
new RangeError(message, fileName)
new RangeError(message, fileName, lineNumber)

RangeError()
RangeError(message)
RangeError(message, options)
RangeError(message, fileName)
RangeError(message, fileName, lineNumber)

Note: RangeError() can be called with or without new. Both create a new RangeError instance.

Parameters

message Optional

Human-readable description of the error.

options Optional

An object that has the following properties:

cause Optional

A property indicating the specific cause of the error. When catching and re-throwing an error with a more-specific or useful error message, this property can be used to pass the original error.

fileName Optional Non-standard

The name of the file containing the code that caused the exception

lineNumber Optional Non-standard

The line number of the code that caused the exception

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-nativeerror-constructors

Browser compatibility

BCD tables only load in the browser

See also

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().

Constructor

RangeError()

Creates a new RangeError object.

Instance properties

RangeError.prototype.message

Error message. Although ECMA-262 specifies that RangeError should provide its own message property, in SpiderMonkey, it inherits Error.prototype.message.

RangeError.prototype.name

Error name. Inherited from Error.

RangeError.prototype.fileName

Path to file that raised this error. Inherited from Error.

RangeError.prototype.lineNumber

Line number in file that raised this error. Inherited from Error.

RangeError.prototype.columnNumber

Column number in line that raised this error. Inherited from Error.

RangeError.prototype.stack

Stack trace. Inherited from 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)
    {
        
    }
}

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)
    {
        
    }
}

Specifications

Browser compatibility

DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariWebView AndroidChrome AndroidFirefox for AndroidOpera AndroidSafari on IOSSamsung InternetDenoNode.js
RangeError

1

12

1

5.5

5

1

1

18

4

10.1

1

1.0

1.0

0.10.0

RangeError

1

12

1

5.5

5

1

1

18

4

10.1

1

1.0

1.0

0.10.0

See also

  • Error
  • Array
  • Number.toExponential()
  • Number.toFixed()
  • Number.toPrecision()
  • String.prototype.normalize()

How do you fix a range error?

The most common way to fix this error is to reduce the number of function calls, or to limit the number of variables that are created. Another option is to use a tracer debugger to keep track of the program's state and find the function that's causing the error.

What is type error in JavaScript?

The TypeError object represents an error when an operation could not be performed, typically (but not exclusively) when a value is not of the expected type. A TypeError may be thrown when: an operand or argument passed to a function is incompatible with the type expected by that operator or function; or.

What is uncaught range error?

The Uncaught RangeError is caused when the browser's hardcoded stack size is exceeded and the memory is exhausted. So, always ensure that the recursive function has a base condition that can stop its execution.

What is eval error in JavaScript?

The EvalError object indicates an error regarding the global eval() function. This exception is not thrown by JavaScript anymore, however the EvalError object remains for compatibility.