BeginnerCoder

Lesson 25 — Full event emitter

on, off and emit

A full event emitter has three methods: on(event, fn) registers a listener, off(event, fn) removes it, emit(event, data) calls every listener of that event. Removing works by keeping all functions except the one you pass — that is why you must keep a reference to the handler (const h = ...).

[1, 2, 3].filter((x) => x !== 2);   // [1, 3]

Why it matters: events let parts of an app talk without knowing about each other — the core of Node and the browser DOM.

Want it another way?
Task

Finish off(): keep every listener except f. After off, the second emit must be silent.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with JavaScript Foundations

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.