Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

CustomEvent — events with custom data

CustomEvent extends Event with detail, allowing application-defined data to reach event listeners.

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

new CustomEvent(type, { detail: null, bubbles: false, cancelable: false, composed: false })

CustomEvent is a Web API interface from the DOM Standard, not ECMAScript. Its constructor accepts an event name and optional settings. The detail option can carry any JavaScript value except undefined. If you omit this option or explicitly pass undefined, Web IDL uses the default value null. Objects are not copied.

Creating an event does not send it: call dispatchEvent() on an EventTarget. Register listeners with addEventListener(). Dispatch is synchronous. The options bubbles, cancelable, and composed default to false. Check API availability in the target browser; the repository's Node version does not establish it.

Examples

{
  const target = new EventTarget();
  target.addEventListener("score", (event) => {
    console.log(`score=${event.detail}`);
  });
  target.dispatchEvent(new CustomEvent("score", { detail: 7 }));
  console.log("done");
}

Result

score=7
done

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