Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

RangeError

An out-of-range value error.

Available since: ES3

Syntax

new RangeError(message)

Parameters

ParameterTypeDescription
messagestringOptional. A description of the error.

Return value

RangeErrorAn error object.

Examples

Built-in methods throw RangeError:

try {
  (3.14).toFixed(200);
} catch (error) {
  console.log(error.name);
}
 
try {
  new Array(-1);
} catch (error) {
  console.log(error.name);
}

Output:

RangeError
RangeError

Your own RangeError:

function setVolume(level) {
  if (level < 0 || level > 100) throw new RangeError('Volume must be 0–100');
  return level;
}
 
try {
  setVolume(150);
} catch (error) {
  console.log(error.message);
}

Output:

Volume must be 0–100

Notes

Infinite recursion also leads to a RangeError with the message Maximum call stack size exceeded.

See also

  • Error — The base error class: created and thrown with throw.
  • TypeError — A wrong type error — for example, calling a method on undefined.

In the tutorial

  • Errors and try...catch — Error handling in JavaScript: try, catch and finally blocks, the error object, the throw statement and built-in error types.

Thanks for reading!

Donate to the site — we work hard for you.