JavaScript EventTarget Constructor

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Web API constructor

What You’ll Learn

The EventTarget() constructor creates objects that can listen for and fire events (same API as MDN EventTarget()). You already use EventTarget every day on window, document, and HTML elements. This tutorial shows how to build your own emitters with new EventTarget(), extends EventTarget, and CustomEvent — with five examples and try-it labs.

01

Create

new EventTarget()

02

Extend

class X extends EventTarget

03

Listen

addEventListener

04

Fire

dispatchEvent

05

Data

CustomEvent

06

Workers

Supported

Introduction

Buttons, inputs, and the window object can all call addEventListener because they implement the EventTarget interface.

With new EventTarget() you get that same ability on a plain JavaScript object — useful for counters, stores, game entities, or any model that should notify listeners when something changes.

💡
Typical pattern

MDN notes you rarely call the constructor alone. More often you extends EventTarget and call super() in your class constructor so instances inherit the event methods.

Continue with Window methods after you understand custom emitters — DOM objects are EventTargets too.

Understanding the EventTarget Constructor

Calling new EventTarget() returns a new object with the standard event methods. No arguments are required.

  • addEventListener(type, listener) — register a handler.
  • removeEventListener(type, listener) — unregister the same function reference.
  • dispatchEvent(event) — fire an Event or CustomEvent.
  • Subclass with extends EventTarget and call super() first.

📝 Syntax

Form of the EventTarget constructor (from MDN):

JavaScript
new EventTarget()

Parameters

  • None.

Return value

A new EventTarget instance.

Common patterns

JavaScript
const target = new EventTarget();

target.addEventListener("ping", (e) => {
  console.log("got", e.type);
});

target.dispatchEvent(new Event("ping"));

// Subclass (usual real-world style):
class Counter extends EventTarget {
  constructor(initialValue = 0) {
    super();
    this.value = initialValue;
  }
}

⚡ Quick Reference

GoalCode
Create an emitternew EventTarget()
Subclassclass X extends EventTarget { constructor(){ super(); } }
Listentarget.addEventListener("change", fn)
Fire a simple eventtarget.dispatchEvent(new Event("change"))
Fire with datanew CustomEvent("change", { detail: value })
Stop listeningtarget.removeEventListener("change", fn)

🔍 At a Glance

Four facts to remember about the EventTarget constructor.

Create
new EventTarget()

No parameters

Extend
super()

Call in subclass

Payload
CustomEvent

Use detail

DOM?
optional

Works without elements

📋 new EventTarget() vs DOM nodes vs custom emitters

new EventTarget()DOM elementHand-rolled emitter
Built-in APIYesYesYou invent it
Needs HTMLNoYesNo
CustomEventYesYesOnly if you build it
Best forApp models / servicesUI interactionsLegacy / Node-only libs

Examples Gallery

Examples follow MDN EventTarget() patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

Create a target, listen, and dispatch.

Example 1 — Basic new EventTarget()

The smallest listen / fire loop.

JavaScript
const target = new EventTarget();

target.addEventListener("hello", () => {
  console.log("Hello from EventTarget");
});

target.dispatchEvent(new Event("hello"));
// Hello from EventTarget
Try It Yourself

How It Works

No DOM node is required. The target only needs a matching event type string on both addEventListener and dispatchEvent.

Example 2 — Counter Class (MDN Style)

Extend EventTarget, call super(), and fire valuechange.

JavaScript
class Counter extends EventTarget {
  constructor(initialValue = 0) {
    super();
    this.value = initialValue;
  }

  emitChange() {
    this.dispatchEvent(
      new CustomEvent("valuechange", { detail: this.value })
    );
  }

  increment() {
    this.value++;
    this.emitChange();
  }

  decrement() {
    this.value--;
    this.emitChange();
  }
}

const counter = new Counter(0);
counter.addEventListener("valuechange", (e) => {
  console.log("value:", e.detail);
});

counter.increment(); // value: 1
counter.increment(); // value: 2
counter.decrement(); // value: 1
Try It Yourself

How It Works

super() sets up the EventTarget internals. Listeners read event.detail for the new value — the MDN counter pattern.

📈 Practical Patterns

CustomEvent data, removing listeners, and once options.

Example 3 — Pass Data with CustomEvent

Use detail for any JSON-friendly payload.

JavaScript
const bus = new EventTarget();

bus.addEventListener("user:login", (e) => {
  console.log(e.detail.name, e.detail.id);
});

bus.dispatchEvent(
  new CustomEvent("user:login", {
    detail: { name: "Ada", id: 42 }
  })
);
// Ada 42
Try It Yourself

How It Works

Plain Event has no payload field. CustomEvent adds detail so listeners receive structured data.

Example 4 — Remove a Listener

Pass the same function reference to remove it.

JavaScript
const target = new EventTarget();

function onTick() {
  console.log("tick");
}

target.addEventListener("tick", onTick);
target.dispatchEvent(new Event("tick")); // tick

target.removeEventListener("tick", onTick);
target.dispatchEvent(new Event("tick")); // (no output)
Try It Yourself

How It Works

Anonymous inline functions cannot be removed later. Keep a named function (or stored reference) when you need cleanup.

Example 5 — Listen Once

Use the once: true option so the handler auto-removes.

JavaScript
const target = new EventTarget();

target.addEventListener(
  "ready",
  () => console.log("ready once"),
  { once: true }
);

target.dispatchEvent(new Event("ready")); // ready once
target.dispatchEvent(new Event("ready")); // (silent)
Try It Yourself

How It Works

Perfect for initialization signals. The listener fires on the first matching event and then unregisters itself.

🚀 Common Use Cases

  • App models — counters, carts, and settings that notify the UI.
  • Mini event buses — decouple modules with typed custom events.
  • Game / animation objects — fire spawn, hit, done without DOM nodes.
  • Workers — same listen/dispatch pattern off the main thread.
  • Teaching the DOM event model — practice without needing HTML first.
  • Not a replacement for framework stores — use when native events are enough.

🧠 How new EventTarget() Works

1

Construct the target

new EventTarget() or super() in a subclass.

Create
2

Register listeners

Map event type strings to callback functions.

Listen
3

Dispatch an event

Create Event / CustomEvent and call dispatchEvent.

Fire
4

Handlers run in order

Matching listeners receive the event object (and detail if set).

📝 Notes

  • Always call super() before using this in a subclass constructor.
  • Event type names are case-sensitive strings ("valuechange""ValueChange").
  • Keep function references if you plan to call removeEventListener.
  • dispatchEvent is synchronous — listeners run before the next line continues.
  • Available in Web Workers (per MDN).
  • DOM nodes already are EventTargets — you do not wrap a button in new EventTarget().

Universal Browser Support

The constructible EventTarget() API is Baseline Widely available in modern browsers (roughly since 2020). It is also available in Web Workers. Internet Explorer does not support new EventTarget().

Baseline · Widely available

EventTarget() constructor

Safe in modern Chrome, Firefox, Safari, and Edge. Prefer extends EventTarget + super() for custom emitters.

Modern Widely available
Google Chrome 64+ · Desktop & Mobile
Full support
Mozilla Firefox 59+ · Desktop & Mobile
Full support
Apple Safari 14+ · macOS & iOS
Full support
Microsoft Edge 79+ · Chromium
Full support
Internet Explorer Not supported · Use a polyfill
No support
Opera 51+ · Modern versions
Full support
EventTarget() Excellent in modern browsers

Bottom line: Use new EventTarget() or class extends EventTarget. Pair with CustomEvent for payloads. Not available in Internet Explorer without a polyfill.

Conclusion

The EventTarget() constructor gives any object the same listen/dispatch API as the DOM. Create a target with new EventTarget(), or more often extends EventTarget and call super(), then fire CustomEvents when state changes.

Continue with Window methods (window is an EventTarget), or explore more JavaScript topics.

💡 Best Practices

✅ Do

  • Call super() first in subclass constructors
  • Use clear event type names ("cart:update")
  • Pass data with CustomEvent detail
  • Store listener references when you need to remove them
  • Prefer { once: true } for one-shot setup events

❌ Don’t

  • Forget super() in an extends EventTarget class
  • Expect anonymous listeners to be removable later
  • Mix up event type spelling between listen and dispatch
  • Assume Internet Explorer supports new EventTarget()
  • Wrap DOM elements in another EventTarget unnecessarily

Key Takeaways

Knowledge Unlocked

Five things to remember about the EventTarget constructor

Build custom emitters with the same API as the DOM.

5
Core concepts
📚 02

Extend

super()

Class
🔊 03

Listen

addEventListener

Pattern
🔥 04

Fire

dispatchEvent

Emit
📦 05

Data

CustomEvent

detail

❓ Frequently Asked Questions

new EventTarget() creates a plain object that can listen for and dispatch events using addEventListener, removeEventListener, and dispatchEvent — without being a DOM element.
Rarely. Most often you call super() inside a class that extends EventTarget, so your own objects can fire custom events. MDN notes explicit new EventTarget() is uncommon outside that pattern.
Elements like button or document already implement EventTarget. The constructor lets you build non-DOM objects (stores, counters, services) that speak the same event API.
Any Event or CustomEvent. CustomEvent is popular because detail can carry data, for example new CustomEvent("valuechange", { detail: 42 }).
Yes. MDN marks the constructor as available in Web Workers, so you can use the same listen/dispatch pattern off the main thread.
It is Baseline Widely available in modern Chrome, Firefox, Safari, and Edge. Internet Explorer does not support constructing EventTarget. Polyfills exist for older environments.
Did you know?

Before constructible EventTarget landed in browsers, developers often attached a hidden DOM node or wrote tiny pub/sub libraries just to get addEventListener-style APIs on plain objects.

Explore Window Methods

Window is a built-in EventTarget — alert, timers, and more.

Window methods →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful