Type addEventListener’s third argument for the teacher and the class light switch: capture, once, passive and signal read by the real browser, including a…
One-off handlers with no manual cleanup, catching an event before its children do, smooth scrolling on phones, and tidy component cleanup: each is one object in the third argument. signal is also the only way to remove an inline function.
The third argument changes when, how often and until when a listener runs
removeEventListener does not remove your listener It needs the very same function AND the same capture flag. An inline function cannot be removed this way; use { signal } + abort().preventDefault() is ignored in a passive listener passive: true promises not to cancel the default action, so the browser ignores it (Chrome logs an error in the console). If you do need to cancel, set passive: false.stopPropagation() on the child cannot stop it.{ capture: true } on .outer, then press the switch The log: .outer first with eventPhase 1, then .toggle (2).{ once: true } on the toggle, press twice The second press still works the lamp, but the browser has already removed the listener.{ passive: true } + tick preventDefault, press the switch defaultPrevented stays false and the lamp still changes: the passive promise wins.removeEventListener, press the switch again The listener still runs: a new function is not the one that was added.