JavaScript · Junior · Question 40 of 75
Why is addEventListener better than el.onclick = handler?
- ✓Correct answer: It allows multiple handlers and options (
once,passive) - 2It's faster because it runs in a separate thread
- 3Only it gives you access to the event object in the handler
Explanation
Reassigning onclick overwrites the previous handler. addEventListener adds them independently and supports the capture phase (capture), one-time handlers and removal via signal (AbortController). Remove handlers with removeEventListener and the same function.