JavaScript Error

Error, TypeError, RangeError, AggregateError, and error.cause. Examples assert name and message, not the engine stack string.

モジュールページの本文は現在英語です。

Error is an ECMAScript built-in. TypeError, RangeError, SyntaxError, and AggregateError are subclasses. This page is about the language Error object, not DOM exceptions such as DOMException, and not Node.js system errors.

message and name are the portable fields to assert. stack exists in Node.js and in most browsers, but its format is engine-specific and is not used as expected output here. JSON.stringify(new Error("boom")) is {} because message is a non-enumerable data property, so logging an error by stringifying it hides the message.

Examples were checked in Node.js 22, which implements these ECMAScript built-ins. See the MDN documentation for the complete API.

JavaScript tutorial and API overview →

API index

APISummary
Error, Error.prototype.message, Error.prototype.causeCreate an Error with a message and an optional cause. Assert name and message; do not hard-code the stack string.
TypeError, RangeError, AggregateErrorThrow and catch built-in Error subclasses. Inspect name, message, and AggregateError.errors, not the stack text.

message, name, and cause

new Error(message) sets name to Error and message to that string. The second argument can be { cause }, an ES2022 option, to attach an underlying error. cause is ordinary data; it is not printed as part of message. Read error.cause when you need the inner error. The example checks typeof stack === "string" without printing the stack text.

Error, Error.prototype.message, Error.prototype.cause

new Error(message)
new Error(message, { cause })
error.name
error.message
error.cause

Create an Error with a message and an optional cause. Assert name and message; do not hard-code the stack string.

ParameterDescription
messageA string stored in error.message.
causeAn optional underlying value, usually another Error, passed as new Error(message, { cause }).

Returns: An Error instance. name is "Error" unless a subclass changes it.

  • JSON.stringify(err) is {} in this engine for a plain Error, because message is not enumerable. Use error.message or a custom formatter.
  • cause is available in Node.js 22. Older engines without the options argument would ignore the second parameter.
javascript
const err = new Error("boom");
console.log(err.name, err.message);
console.log(JSON.stringify(err));
console.log(typeof err.stack === "string");
const caused = new Error("outer", { cause: new TypeError("inner") });
console.log(caused.message, caused.cause instanceof TypeError, caused.cause.message);

Expected output

Error boom
{}
true
outer true inner

MDN documentation →

TypeError, RangeError, and AggregateError

TypeError signals a value of the wrong type, RangeError a value outside an allowed range, and AggregateError a list of errors, for example from Promise.any. catch can test the class with instanceof. AggregateError stores the list in the errors property. The example prints names and messages only.

TypeError, RangeError, AggregateError

new TypeError(message)
new RangeError(message)
new AggregateError(errors, message)
error instanceof Error

Throw and catch built-in Error subclasses. Inspect name, message, and AggregateError.errors, not the stack text.

ParameterDescription
messageThe string stored on the error.
errorsFor AggregateError, an iterable of inner errors.

Returns: An instance of the corresponding subclass. Each is also an instanceof Error.

  • The catch block confirms that a TypeError is an Error, but the example never prints e.stack.
javascript
try {
  throw new TypeError("bad type");
} catch (error) {
  console.log(error instanceof TypeError, error instanceof Error, error.name, error.message);
}
try {
  throw new RangeError("out of range");
} catch (error) {
  console.log(error.name, error.message);
}
const aggregated = new AggregateError([new Error("one"), new TypeError("two")], "batch failed");
console.log(aggregated.name, aggregated.message, aggregated.errors.map((item) => item.message).join(","));

Expected output

true true TypeError bad type
RangeError out of range
AggregateError batch failed one,two

MDN documentation →