Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท JSON, Errors and Reflection

TypeError, RangeError, SyntaxError and other error types

Reference for the built-in JavaScript error types: TypeError, RangeError, ReferenceError, SyntaxError, URIError and EvalError, what triggers each and how to handle them.

Besides plain Error, JavaScript defines a handful of built-in subclasses. Each one signals a category of problem, and error.name tells you which: TypeError, RangeError, ReferenceError, SyntaxError, URIError and EvalError. All of them inherit from Error, so instanceof Error is true for every one, and all accept (message, { cause }).

Knowing the categories helps you read error messages quickly and choose the right type when you throw your own. A fifth family, AggregateError, is covered on its own page.

JavaScript
try {
  null.length;
} catch (error) {
  console.log(error instanceof TypeError); // true
}

Try it

JS Triggering each built-in type

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Choosing the right type for your own errors

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The error types

Type Meaning Typical causes
Error Generic base type. Your own application errors.
TypeError A value is not of the expected type, or an operation is not allowed on it. Reading a property of null or undefined, calling something that is not a function, mixing BigInt and Number, assigning to a read-only property in strict mode, invalid arguments such as a non-iterable in for...of.
RangeError A value is a valid type but outside the allowed range. new Array(-1), toFixed(101), "x".repeat(-1), Number.prototype.toString(1), invalid dates in toISOString(), and too-deep recursion ("Maximum call stack size exceeded" in most engines).
ReferenceError A variable name cannot be resolved. Using an undeclared variable, or reading a let/const before its declaration runs (the temporal dead zone).
SyntaxError Text is not valid for the parser. Invalid source code, JSON.parse() of bad JSON, new RegExp("(").
URIError A URI function received malformed input. decodeURIComponent("%"), encodeURIComponent("\uD800").
EvalError Historic; engines no longer throw it. Kept for compatibility only.

Usage notes

  • Messages differ between engines, so branch on error.name or instanceof, never on message text.
  • SyntaxError for source code is thrown at load time, before any of the script runs, so you cannot catch it in the same script. It is catchable when it comes from eval(), new Function(), JSON.parse() or new RegExp().
  • A ReferenceError is about names, a TypeError is about values. typeof undeclared does not throw, but undeclared.x does.
  • Throw the closest type from your own code: TypeError for wrong argument types, RangeError for numbers out of bounds. Use your own Error subclass for domain problems.
  • Do not rely on RangeError for stack overflow detection. Handling it is unreliable; fix the recursion instead.
  • DOMException is separate. Browser APIs throw it with names such as "AbortError" or "NotFoundError", inspected via error.name.
  • Strict mode turns silent failures into TypeErrors, such as writing to frozen objects or non-writable properties.

Browser support

All of these error types are supported in every browser and runtime.