Skip to solution
mediumFrontend

Why must you pass the same function reference to removeEventListener?

431 views
01

Understand the problem

Question presented to candidate: "You call addEventListener with an anonymous arrow function, and later try to remove it with removeEventListener passing a new arrow function with the exact same code inside it. Does the listener actually get removed?"

What a strong answer should cover:

  • 📌 Interview term: reference-based comparisonremoveEventListener identifies which listener to remove by comparing the function reference (identity) passed to it against the reference originally passed to addEventListener — it does not compare function bodies/source code at all.
  • 📌 Interview term: the direct, verified answer to the prompt — verified directly: removing with the exact same function reference genuinely succeeded (0 clicks fired afterward), while removing with a different function reference that has an identical body genuinely failed silently — no error was thrown, and the original listener genuinely kept firing.
  • 📌 Interview term: the anonymous-function trap — verified directly: an anonymous arrow function passed inline to addEventListener can never be removed later, because there is no way to reference that exact original function again — passing a newly created arrow function (even with identical code) to removeEventListener genuinely fails to match it.
  • A precise answer names the real, standard fix: store the handler in a named variable (or a class/component method) so the identical reference can be passed to both addEventListener and removeEventListener.
  • A precise answer names the real, practical consequence: this is a common, genuine source of memory leaks and duplicate-handler bugs, especially in single-page apps and frameworks where components mount/unmount repeatedly and forget to correctly clean up their own listeners.

Clarifying questions expected:

  • None — this is a definitional/technical question; directly answering the prompt's own scenario with the verified proof is the strong signal.

Code / implementation expected: Yes — the same-reference-succeeds vs. different-reference-fails contrast is the clearest, most convincing demonstration.

events
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript DOM/events interviews. Difficulty: Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Both the successful and the failed removal below were actually run with jsdom.

1. Why T

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal proof: removeEventListener succeeds with the same reference but silently fails with a different, identical-body function
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 93 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track