Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Error.message — the error message text

An Error instance's message property holds its message text without the error name. Learn how error.message differs from error.toString() and what happens when the message is omitted.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

const error = new Error(message);
error.message

Error.message here denotes the message property of an error instance, accessed as error.message, not a static property of the Error constructor. It belongs to ECMAScript, not to a Web API.

When called as new Error(message) with an argument other than undefined, the constructor applies the ToString operation. Only if conversion succeeds does it store the result in an own, non-enumerable message property that is writable and configurable. For example, new Error(Symbol()) throws a TypeError during conversion, so the call does not return an instance. If the argument is omitted or is undefined, the constructor creates no own message property; with the prototype unchanged, reading it returns the inherited Error.prototype.message, whose initial value is the empty string "".

error.message contains only the message. In contrast, error.toString() for an ordinary Error with a nonempty message combines the name and message as Error: Invalid input. When the message is empty, it returns only the name Error.

For predictable examples, supply your own message, as below. Do not base program logic on the exact wording of engine-generated messages: their text can differ between implementations.

Examples

const error = new Error("Invalid input");
console.log(error.message);
console.log(error.toString());
console.log(JSON.stringify(new Error().message));

Result

Invalid input
Error: Invalid input
""

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources