JavaScript Error Types

Beginner
6 min read
Updated: Nov 2026
Overview

Definition

JavaScript ships several built-in Error subclasses. They all extend Error, so err instanceof Error is true, while err.name tells you the specific kind.

This overview keeps every common type in one place. For throw / try / catch basics, start at the Error hub.

Syntax

Create and throw a specific type:

JavaScript
throw new TypeError("x is not a function");
throw new RangeError("Invalid array length");
throw new AggregateError([e1, e2], "All failed");

Parameters

ParameterDescription
messageOptional string explaining the problem (all Error types).
options.causeOptional underlying error for chaining (modern engines).
errors (AggregateError)Required iterable of errors collected together.

Return Value

Return ValueDescription
Error instanceAn object with name, message, and usually stack. Throw it to report failure.
TypeWhen to use
ErrorGeneral-purpose error.
AggregateErrorWraps several errors (for example Promise.any).
EvalErrorHistorical; rarely thrown by modern engines.
RangeErrorA number is outside the allowed range.
ReferenceErrorA variable does not exist.
SyntaxErrorInvalid code syntax (often from parsing).
TypeErrorWrong type or invalid operation on a value.
URIErrorBad URI component encoding/decoding.

JavaScript Examples

Example 1 — TypeError

Wrong type of value.

JavaScript
try {
  const x = null;
  x();
} catch (err) {
  console.log(err.name);
  console.log(err instanceof TypeError);
}
Output
TypeError
true

Example 2 — RangeError

A number outside the allowed range.

JavaScript
try {
  const arr = new Array(-1);
} catch (err) {
  console.log(err.name);
}
Output
RangeError

Example 3 — AggregateError

Collect several errors in one object.

JavaScript
const err = new AggregateError(
  [new Error("one"), new Error("two")],
  "Both failed"
);
console.log(err.name);
console.log(err.errors.length);
console.log(err.message);
Output
AggregateError
2
Both failed

Frequently Asked Questions

They are built-in constructors that extend Error for specific problems — TypeError for wrong types, RangeError for bad numeric ranges, and so on.
Use the most specific built-in that fits. Fall back to Error for general failures. Prefer AggregateError when reporting many errors together.
Yes. TypeError, RangeError, and the others inherit from Error.
It holds an errors array. Promise.any rejects with AggregateError when every promise rejects.
Return to the Error hub for try/catch, or open Error methods for toString and isError.

Practice Error methods

Learn toString and Error.isError next.

Error Methods →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful