Skip to the page

The DOM and events · mid

Listener options, removing listeners and custom events

addEventListener options (once, passive, capture, signal) control how a listener behaves; an AbortController signal removes many listeners at once; CustomEvent lets your components talk through the DOM.

The notes

What you must know

  • { passive: true } promises no preventDefault, so scrolling and touch stay smooth (Chrome makes touch/wheel listeners on window passive by default).
  • { once: true } removes itself after the first call. { signal } removes it when the controller aborts.
  • removeEventListener needs the same function reference and the same capture flag; an inline arrow can't be removed.
  • new CustomEvent('cart:add', { detail, bubbles: true }) + dispatchEvent; listeners read e.detail.

The red pen

Common traps: mistake → fix

  • Mistake: "removeEventListener(type, () => fn())" works

    Fix: a new arrow is a different function, so nothing is removed.

Easy to hard

Questions interviewers ask, easy to hard

  1. Why are passive listeners faster?
  2. How do you clean up 5 listeners in one line?
  3. When would you use a CustomEvent?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview