Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

EventTarget.dispatchEvent(): synchronous event dispatch

dispatchEvent(event) synchronously dispatches an event to a target. Its Boolean result indicates cancellation, not whether listeners exist.

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

target.dispatchEvent(event)

target.dispatchEvent(event) dispatches an Event object to an EventTarget. Listeners run synchronously; the method does not await asynchronous work started by listeners. The event has isTrusted === false.

The result is false when the event is canceled; otherwise it is true. preventDefault() cancels only with cancelable: true and outside a passive listener. stopPropagation() does not mean cancellation.

In a DOM tree, capture and optional bubbling (bubbles) apply. A standalone new EventTarget() has no parent. Listener exceptions are reported but do not propagate to the caller. Redispatching the object currently being dispatched causes InvalidStateError.

The example demonstrates synchronous ordering and cancellation. Application code can use the result to skip saving.

Examples

const target = new EventTarget();
const log = [];

target.addEventListener("save", (event) => {
  log.push("listener");
  event.preventDefault();
});

log.push("before");
const accepted = target.dispatchEvent(
  new Event("save", { cancelable: true })
);
log.push(`after: ${accepted}`);
console.log(log.join(" | "));

Result

before | listener | after: false

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