All topics
DOMbeginner

preventDefault vs stopPropagation

Two distinct event methods often confused: one cancels the browser's default action for an event, the other stops the event from traveling further through the DOM.

event.preventDefault() and event.stopPropagation() are both commonly called inside event handlers, but they do genuinely different things, and confusing them is one of the most common practical mistakes developers make with the DOM event system — which is exactly why interviewers like pairing them in a single question.

preventDefault() is like telling airport security 'don't let this specific passenger board the plane' — it stops one particular downstream consequence but doesn't stop word of the incident from spreading through the terminal (propagation). stopPropagation() is the opposite: it's like cutting the radio silence so nobody further down the chain of command even hears about the incident, but the passenger might still board the plane (default action) if nobody told security to stop them.

Key Concepts

1
preventDefault() cancels whatever default browser behavior is normally associated with that event, without affecting whether the event continues to propagate through the DOM at all. Classic examples: calling it inside a form's submit handler prevents the actual page navigation/reload that would otherwise happen; calling it inside an anchor tag's click handler prevents the browser from following the href and navigating away; calling it during a keydown handler can prevent a character from actually being typed into an input. The event still bubbles normally to ancestor listeners unless you separately call stopPropagation() too.
preventDefault()submitclickhrefkeydown
2
stopPropagation() does the opposite kind of thing: it stops the event from continuing its journey through the DOM tree (bubbling further up, or capturing further down), so ancestor (or descendant, during capturing) listeners never see it — but it has no effect at all on the browser's default action for that event, which still happens normally unless preventDefault() is also called.
stopPropagation()preventDefault()
3
Because the two are independent, you often need both together — for instance, a custom dropdown menu's toggle button might call preventDefault() to stop a wrapping <a> tag's navigation and stopPropagation() to stop a document-level 'click outside to close' listener from immediately closing the dropdown it just opened. A related but distinct method, stopImmediatePropagation(), additionally prevents any other listeners registered on that *same* element for that same event from running at all, even ones attached before it in the same phase.
preventDefault()<a>stopPropagation()stopImmediatePropagation()