CKTIS / JavaScript
ErrorEvent.error and ErrorEvent.message: error value vs. message
`message` is the event’s diagnostic text, while `error` is the associated JavaScript value. That value need not be an `Error` object.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
new ErrorEvent(type)
new ErrorEvent(type, { message, error })
event.message
event.errorErrorEvent.error/message refers to two read-only properties on an ErrorEvent instance. event.message is a string describing the problem; event.error is the associated JavaScript value. It can be an Error object, a primitive such as 0, null, or undefined. Do not assume it has its own message or stack trace. ErrorEvent is a web platform interface, not an ECMAScript 2024 built-in.
new ErrorEvent(type, options) initializes the two properties independently. Under the HTML standard, an omitted message defaults to "" and an omitted error defaults to undefined. If the supplied value is an Error object, event.error.message belongs to that object and does not have to match event.message.
When the browser reports an uncaught script exception on the window, a listener registered with window.addEventListener("error", handler) receives one ErrorEvent. For the same event, window.onerror receives five positional arguments: (message, source, lineno, colno, error). Not every error event is an ErrorEvent: a resource load failure on an element does not guarantee these properties. The browser can report an unhandled promise rejection through a separate unhandledrejection event with a reason property. It does not dispatch that event if the script’s errors are muted or if the rejection is handled before notification.
When a classic script’s error details are muted, as can happen with a cross-origin script loaded without CORS, the report has message set to "Script error." and error set to null. Treat message text as diagnostic information, not a stable error code. Outside the browser, check whether the runtime exposes ErrorEvent.
Examples
{
// Use the native constructor when available; otherwise model only these two properties.
const ErrorEvent = globalThis.ErrorEvent ?? class extends Event {
#message;
#error;
constructor(type, { message = "", error } = {}) {
super(type);
this.#message = String(message);
this.#error = error;
}
get message() { return this.#message; }
get error() { return this.#error; }
};
const original = new Error("Invalid input");
const event = new ErrorEvent("error", {
message: "Validation failed",
error: original,
});
console.log(event.message);
console.log(event.error === original);
console.log(event.error.message);
}Result
Validation failed true Invalid input
Run this in Node.js 24 or a browser console. If native `ErrorEvent` is unavailable, the example uses a local stand-in that models only the two properties shown. It creates a synthetic event without throwing an exception or dispatching the event. The `true` line confirms object identity; the first and last lines show two different messages.
Related lessons
- JavaScript · Lesson 23 · Advanced · Version: 4