Skip to the solution
MediumFrontend6 min read

Why must you pass the same function reference to removeEventListener?

431 views

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 comparison β€” removeEventListener 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

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

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

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 93 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

What does `using` / `await using` do for Explicit Resource Management (ES2024)?

Continue