A web page without events is just a poster. Event Listeners are what transform static documents into interactive applications.
1The Listener Pattern
In modern JavaScript, we avoid inline event attributes (like onclick=...) in favor of addEventListener. This method allows you to attach multiple listeners to a single element without overwriting existing ones. It separates your structure (HTML) from your behavior (JS), making your code much easier to maintain and debug. Every time a user interacts with the page, the browser creates an Event Object and passes it to your function, providing context about the action.
2Controlling Browser Behavior
Some HTML elements have default behaviors—links navigate, and forms refresh the page on submission. In modern web development, we often want to override these behaviors to keep the user on our page. event.preventDefault() is the key tool for this. By intercepting these events, we can process form data using fetch APIs or update the UI without a jarring page reload, enabling the smooth experience of Single Page Applications (SPAs).
