preventDefault vs stopPropagation vs stopImmediatePropagation
BeginnerJavaScript interview questionBy UIRound, updated 17 November 2026
Short answer
preventDefault() cancels what the browser would do by itself, such as following a link or submitting a form. It does not affect other listeners. stopPropagation() stops the event travelling to further elements, but other listeners on the current element still run. stopImmediatePropagation() does that and also skips the remaining listeners on the current element. The first is about the browser; the other two are about your listeners.
Two separate things an event does
When you click a link, two independent things happen:
- Propagation. The event travels through the DOM: down from
windowto the link in the capture phase, then back up in the bubble phase. Every matching listener on the way runs. Event bubbling vs capturing explains the phases. - The default action. After dispatch finishes, the browser does its built-in behaviour for that event: it navigates to the link's
href.
preventDefault controls the second. stopPropagation and stopImmediatePropagation control the first. Calling one has no effect on the other.
Side by side
| Cancels the default action | Stops listeners on other elements | Stops later listeners on the same element | |
|---|---|---|---|
preventDefault() |
Yes, if the event is cancelable | No | No |
stopPropagation() |
No | Yes | No |
stopImmediatePropagation() |
No | Yes | Yes |
"Later" means listeners added after the current one with addEventListener, because listeners on one element run in the order they were added.
const button = document.querySelector("button");
button.addEventListener("click", (e) => {
console.log("first");
e.stopImmediatePropagation();
});
button.addEventListener("click", () => console.log("second")); // skipped
document.body.addEventListener("click", () => console.log("body")); // skipped
// Click logs only "first".
// With stopPropagation() instead, it logs "first" and "second".preventDefault and default actions
Common defaults you cancel:
| Event | Default action |
|---|---|
click on <a href> |
Navigate to the link |
submit on a <form> |
Send the form and load a new page |
click on a checkbox |
Toggle its checked state |
keydown in an input |
Insert the character |
contextmenu |
Open the browser's right-click menu |
wheel, touchmove |
Scroll the page |
Only cancelable events can be prevented; check event.cancelable. scroll itself is not cancelable, because it fires after the scroll has already happened. To block scrolling you cancel wheel or touchmove, or use CSS such as overflow: hidden or touch-action.
You can tell whether someone already cancelled an event with event.defaultPrevented. A delegated router uses it to respect other handlers:
document.addEventListener("click", (event) => {
const link = event.target.closest("a[href^='/']");
if (!link || event.defaultPrevented) return;
if (event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey) return; // let new-tab clicks work
event.preventDefault();
navigate(link.getAttribute("href"));
});Two timing rules. Cancel submit on the form, not click on the button, because pressing Enter also submits. And cancel keydown, not keyup, because by keyup the character is already typed.
Passive listeners ignore preventDefault
A listener added with { passive: true } promises it will not cancel the event, so the browser can start scrolling without waiting for your code. Calling preventDefault() inside it does nothing, and Chrome logs a console warning.
Browsers make touchstart, touchmove, wheel and mousewheel listeners passive by default when they are added to window, document or document.body. If your custom swipe handler on document cannot stop the page scrolling, that is why. Pass { passive: false } explicitly, or attach the listener to the element itself. The addEventListener options answer covers passive in detail.
return false
Returning false cancels the default action only in a handler set with an on... property or an inline attribute:
<a href="/delete" onclick="return confirm('Delete?')">Delete</a>If confirm returns false, the navigation is cancelled. It does not stop propagation. In a listener added with addEventListener, the return value is ignored, so return false does nothing at all.
jQuery is the source of the confusion: in a jQuery handler, return false calls both preventDefault and stopPropagation. React does not support return false; call e.preventDefault().
How stopPropagation breaks delegation
Event delegation relies on events reaching an ancestor. A single stopPropagation() in a child silently breaks every listener above it.
// A dropdown closes when you click anywhere else
document.addEventListener("click", closeOpenMenus);
// Elsewhere, a card stops propagation to avoid triggering its parent row
card.addEventListener("click", (e) => {
e.stopPropagation();
openCard();
});
// Clicking a card no longer closes menus, and analytics on document never sees the click.Better options:
- Let the event bubble and check the target in the parent. The row handler can skip clicks with
if (event.target.closest(".card")) return;. - Use
defaultPreventedor your own flag as a signal that a child handled the event, instead of hiding it. - Listen in the capture phase on
documentfor global behaviour such as click-outside, so a child stopping bubbling cannot block it.
Reach for stopPropagation when you truly need to isolate a widget, and say why in a comment.
Practise it
Run the order yourself in Predict event propagation order and Predict addEventListener results, which includes stopImmediatePropagation. Then implement the logic in Build a synthetic event system, and try the React cases in Stop clicks inside a portal modal, Predict React event order and Implement event delegation for a dynamic list.
Common follow-up questions
Does preventDefault stop the event bubbling?
No. The event keeps travelling and every other listener still runs. They can see that it was cancelled through event.defaultPrevented.
Does stopPropagation in a capture listener stop the target's listeners?
Yes. If an ancestor's capture listener calls stopPropagation(), the event never reaches the target or the bubble phase. Other listeners on that same ancestor still run.
How do I know whether my dispatched event was cancelled?
element.dispatchEvent(event) returns false if the event was cancelable and a listener called preventDefault(). Create it with new CustomEvent("save", { cancelable: true }), then skip your default behaviour when the result is false.
Do these work the same in React?
React's event object has the same three methods and passes them through to the native event. Because React listens at the root container, a React stopPropagation() cannot stop native listeners on elements between the target and the root, since they have already run.
Mistakes to avoid
Using
stopPropagationto stop a link navigating or a form submittingThat needs
preventDefault.Calling
preventDefaultin a passive listener and wondering why scrolling continuesWriting
return falsein anaddEventListenercallbackCancelling
clickon a submit button instead ofsubmiton the formStopping propagation by habit and breaking delegated listeners, click-outside handlers and analytics
Sources
Practise it in code
Interviewers rarely stop at the definition. These questions open in an editor with tests.
- Predict event propagation order: capturing, target, bubbling and
stopPropagationEasy25 min - Predict
addEventListenerresults: adding a listener twice,once,captureon removal andstopImmediatePropagationEasy20 min - Build a synthetic event system with root delegation, capture and bubble phases, and
stopPropagationHard60 min - Stop clicks inside a portal modal from triggering the parent's
onClickMedium25 min - Predict React event order: capture and bubble handlers,
stopPropagation, and native listeners on the same elementsMedium25 min - Implement event delegation for a dynamic listEasy15 minFree