Skip to the solution
EasyFrontend5 min read

What is the difference between preventDefault and stopPropagation?

666 views

The problem

Question presented to candidate: "If you call only preventDefault() inside a click handler, does the event still reach a listener on a parent element? And if you call only stopPropagation(), does the browser still perform its default action for that click?"

What a strong answer should cover:

  • πŸ“Œ Interview term: preventDefault() β€” cancels the browser's built-in default action for the event (following a link, submitting a form, checking a checkbox) β€” it does not affect whether the event continues traveling to ancestor listeners.
  • πŸ“Œ Interview term: stopPropagation() β€” stops the event from continuing to travel through the remaining capture/bubble phases to any further ancestors β€” it does not cancel the browser's own default action.
  • πŸ“Œ Interview term: the direct, verified answer to the prompt β€” calling only preventDefault() still let an ancestor's listener genuinely fire (propagation continued), while event.defaultPrevented became genuinely true; calling only stopPropagation() genuinely stopped the ancestor's listener from firing at all, while event.defaultPrevented genuinely stayed false β€” verified directly, proving the two are independent.
  • A precise answer names that both can be called together in the same handler when both effects are needed simultaneously (a common real pattern for a custom-styled link/button that should neither navigate nor let the click bubble to an outer handler).
  • A precise answer names stopImmediatePropagation() as a related, stricter variant: it stops propagation to ancestors and prevents any remaining listeners on the same element from running, unlike plain stopPropagation().

Clarifying questions expected:

  • None β€” this is a definitional/comparison question; directly answering both halves of the prompt's own two questions with the verified proof is the strong signal.

Code / implementation expected: Yes β€” the two isolated tests (preventDefault-only vs. stopPropagation-only) are the clearest, most convincing way to show they are independent.

  • events

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

5 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal, isolated proof that preventDefault and stopPropagation act completely independently

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 17 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextEasy

What are `let`, `const`, and `var`? What are their differences?

Continue