Example 1 — Basic dispatchEvent()
Create a target, listen, then fire a simple event.
const target = new EventTarget();
target.addEventListener("hello", () => {
console.log("Hello from dispatchEvent");
});
target.dispatchEvent(new Event("hello")); How It Works
- Make a target.
new EventTarget()works without the DOM. - Register a listener. Type
"hello"must match the event type. - Create the event.
new Event("hello")initializes the type. - Dispatch. The listener runs immediately; return value is
true.

