Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

MessageEvent.data — message payload

MessageEvent.data is a read-only property that exposes a message's payload. Its type depends on the messaging mechanism, so do not assume the value is always text.

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

event.data

MessageEvent.data exposes the data supplied by a message sender. Read it from an event instance using event.data, not directly from the MessageEvent constructor.

The value can have different types depending on the mechanism that created the event. Check the expected type and structure before using it; the name data alone does not imply text or a particular format.

The property is read-only: it is not a way to replace the event's payload by assigning to event.data. When creating an event with new MessageEvent("message", { data: value }), supply the payload through the constructor options. Omitting the data option produces null.

This is a Web API, not a feature tied to a particular ECMAScript edition. MDN reports broad availability of the property since July 2015 and of the constructor used in the example since August 2016. The example creates an event locally without sending a message.

Examples

const event = new MessageEvent("message", {
  data: { text: "hello", count: 3 }
});

console.log(event.data.text);
console.log(event.data.count);
console.log(new MessageEvent("message").data === null);

Result

hello
3
true

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