JavaScript Events Interview Questions and Answers
Understand event listeners, propagation, delegation, and how browser events drive interactive interfaces.
Frequently Asked Questions
Important questions are highlighted in the sections below; select one to jump to its answer.
Theoretical JavaScript Interview Questions
When you click a button inside a div, the click event fires on the button first, then bubbles up through the div, body, and document - that's event bubbling. It's the default behavior for almost all events and is actually really useful for event delegation, where you attach one listener on a parent instead of dozens on each child. You can stop it with event.stopPropagation(), but only do that when you truly need to - it makes debugging event flows much harder.
Event capturing is the opposite of bubbling - the event travels down from the document root to the target element before it fires on that element. In practice you rarely need it, but it's useful when you want to intercept an event before it reaches its target. To opt in, pass true (or { capture: true }) as the third argument to addEventListener - by default it's false, meaning you're in the bubbling phase.
Capturing goes top-down (document to target), bubbling goes bottom-up (target to document) - they're the two phases of the same event journey. Your addEventListener calls use bubbling by default, which is what you want 99% of the time. The real-world implication: event delegation relies on bubbling, and if you ever call stopPropagation() on a child element, the parent's bubbling listeners won't fire.
An event is the browser telling you something happened - a click, a keypress, a network response completing, the page loading. JavaScript is event-driven by design, so almost everything you do in the browser is in response to an event. You wire up responses using addEventListener, and the event object passed to your handler contains useful info like which element was clicked, which key was pressed, and mouse coordinates.
preventDefault() tells the browser "I'll handle this myself, don't do what you'd normally do." Classic use cases: stopping a form from submitting so you can validate first, preventing a link from navigating for client-side routing, or disabling a right-click context menu. It does not stop bubbling - that's stopPropagation(). Many developers confuse these two, so it's worth keeping them straight.
Event bubbling means that when an event fires on an element, it propagates upward through the DOM tree to all its ancestor elements. For example, clicking a button inside a div will also trigger any click listeners on that div and on the document. You can stop this with event.stopPropagation() inside the handler.
Event capturing is the opposite of bubbling. The event travels from the document root down to the target element before the target handles it. You opt into capturing by passing true as the third argument to addEventListener. The full event lifecycle is: capturing (top-down), target, then bubbling (bottom-up).
Event delegation is a pattern where a single event listener is attached to a parent element to handle events for all its children, relying on event bubbling. It is useful because it reduces the number of listeners (better performance for large lists), and it automatically handles dynamically added child elements without needing to attach new listeners each time.
onclick is an element property that can only hold one handler at a time. Assigning a new function overwrites the previous one. addEventListener can attach multiple independent handlers for the same event without overwriting any of them. It also supports capturing phase listeners and can be removed with removeEventListener, making it the preferred approach.
Call event.stopPropagation() inside an event handler to prevent the event from bubbling up (or capturing down) to ancestor elements. To also prevent any other listeners on the same element from running, use event.stopImmediatePropagation(). To prevent the browser's default action (like following a link), use event.preventDefault(). These methods can be combined as needed.
event.stopPropagation() stops the event from moving to parent or child elements in the event flow. event.preventDefault() stops the browser's default action, like following a link or submitting a form. One controls event movement, while the other controls browser behavior, and sometimes both are used together.
The browser dispatches a `click` event, and registered listeners for that event can run. The event also participates in the DOM event flow unless propagation is stopped.
`addEventListener` registers a callback for a particular event type on an event target. Multiple listeners can be registered without replacing an element's existing handlers.
The event object describes the dispatched event, including properties such as `type`, `target`, and `currentTarget`. Some event types provide additional data, such as pointer coordinates or keyboard keys.
Call `removeEventListener` with the same event type, callback function reference, and matching capture setting used to register it. An anonymous function cannot be removed later unless its reference was saved.
During capture, an event travels from the document toward its target; during bubbling, it travels from the target back through ancestors. A listener can opt into capture with the `capture` option, while not all event types bubble.
A listener on a common ancestor can handle bubbling events from many descendants by inspecting `event.target` or using `closest()` to find a matching child. It reduces per-child listeners and also works for matching children added later.
`stopPropagation()` prevents the event from continuing through the propagation path, while `preventDefault()` cancels the browser's default action when the event is cancelable. Neither call automatically performs the other's behavior.
`target` is the original event target, which may be a descendant of the listener's element. `currentTarget` is the element whose listener is currently running.
JavaScript Practical and Coding Interview Questions
Problem: Create a list with two buttons, attach one delegated click listener to the list, and deterministically dispatch a click on the second button. Log the selected button's `data-item` value without requiring a real user interaction.
const list = document.createElement("ul");
list.innerHTML = '<li><button data-item="alpha">Alpha</button></li><li><button data-item="beta">Beta</button></li>';
document.body.append(list);
list.addEventListener("click", (event) => {
const button = event.target.closest("button[data-item]");
if (button && list.contains(button)) {
console.log(button.dataset.item);
}
});
list.querySelectorAll("button")[1].dispatchEvent(
new MouseEvent("click", { bubbles: true })
);
list.remove();