They control two independent things. preventDefault() cancels the browser's default action (navigating, submitting, checking a box) but the event keeps propagating to other listeners. stopPropagation() stops the event travelling to further nodes in the capture/bubble path, but the default action still happens. stopImmediatePropagation() additionally skips the remaining listeners on the current element. Use them together only when you need both effects.
<div id="card">
<a id="link" href="/details">Details</a>
</div>
card.addEventListener('click', () => console.log('card clicked'));
link.addEventListener('click', (e) => {
e.preventDefault(); // no navigation
// 'card clicked' still logs: the event bubbles
});
link.addEventListener('click', (e) => {
e.stopPropagation(); // card's listener doesn't run
// the browser still navigates to /details
});
| Default action | Other nodes' listeners | Other listeners on same node | |
|---|---|---|---|
preventDefault() | Cancelled | Run | Run |
stopPropagation() | Happens | Skipped | Run |
stopImmediatePropagation() | Happens | Skipped | Skipped (later-registered ones) |
Both preventDefault + stopPropagation | Cancelled | Skipped | Run |
stopImmediatePropagationbutton.addEventListener('click', (e) => {
if (!isAllowed()) e.stopImmediatePropagation(); // guard runs first, blocks the rest
});
button.addEventListener('click', doTheThing); // skipped when blocked
Listeners on the same node run in registration order, so this only affects listeners added after the current one.
preventDefault when you replace native behaviour:
fetch.dragover must be prevented to allow a drop).stopPropagation rarely, and deliberately:
event.target.closest('button') in the card's handler is often cleaner.Escape shouldn't also close the outer dialog.stopPropagation is riskyIt's invisible to the code it affects. Stopping a click at a component boundary can silently break:
document.Prefer letting events bubble and having handlers ignore events they don't own, or mark events (e.defaultPrevented, a WeakSet of handled events) instead of stopping them.
e.stopPropagation() on a synthetic event stops React propagation and calls the native method too. Because React 17+ listens on the root container, this now also stops native bubble listeners on document (in React 16, which listened on document, it couldn't). Native listeners attached directly to inner elements still run before React handlers.@click.prevent and @click.stop modifiers map to these methods.return false in a handler did both, which caused many accidental propagation bugs in older code.stopPropagation to prevent navigation or form submission. It doesn't.preventDefault to keep parent handlers from running. It doesn't.await: dispatch is over by then.stopImmediatePropagation for same-element listeners.stopPropagation should be a last resort, and alternatives like target checks and defaultPrevented.Spotted a mistake or a rights concern? Report an issue