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), whileevent.defaultPreventedbecame genuinelytrue; calling onlystopPropagation()genuinely stopped the ancestor's listener from firing at all, whileevent.defaultPreventedgenuinely stayedfalse— 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 plainstopPropagation().
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.