How do event listeners work in JavaScript?
You register a function against a type of event on a target, and the browser calls that function when the event happens. The addEventListener method takes the name of the event as a string, the function to call, and an optional options object.
// Node ships the same EventTarget API that the browser uses, so this runs here too.
const button = new EventTarget();
let clicks = 0;
function onClick(event) {
clicks += 1;
console.log('clicked', clicks, 'type:', event.type);
}
button.addEventListener('click', onClick);
button.dispatchEvent(new Event('click'));
button.dispatchEvent(new Event('click'));
console.log('total clicks:', clicks); // 2The options object is the part that is worth remembering, because it controls how the listener behaves. The capture option decides which phase of the event propagation the listener runs in, and it is false by default, which means the bubble phase. The once option removes the listener after it has fired for the first time. The passive option promises that the listener will not call preventDefault, and that promise lets the browser keep scrolling smoothly while the listener runs.
Listeners fire in the order that you added them, and the same function can be registered for the same event more than once, in which case it runs once for every registration. That last point is the source of a very common bug, where a component that registers its listener again on every render ends up handling a single click several times.
There is also the older approach of assigning a handler property, such as element.onclick. It is simpler, but it replaces any previous handler instead of adding to it, and it does not support the capture, once, or passive options. New code should use addEventListener.