Skip to the solution
EasyFrontend5 min read

What is event bubbling and capturing?

459 views

The problem

Question presented to candidate: "When you click a deeply nested element, in what order do event listeners on its ancestors actually fire β€” and does it matter whether those listeners were registered for capturing or bubbling?"

What a strong answer should cover:

  • πŸ“Œ Interview term: the capture phase β€” the event first travels downward, from window/document toward the actual target element, triggering any listener registered with the third addEventListener argument set to true (or { capture: true }) along the way.
  • πŸ“Œ Interview term: the target phase β€” the event reaches the actual element it originated on.
  • πŸ“Œ Interview term: the bubble phase β€” the event then travels back upward, from the target back out through every ancestor to document, triggering any listener registered normally (capture false, the default).
  • πŸ“Œ Interview term: the real, verified firing order β€” verified directly with jsdom, registering listeners on both phases on nested outer/inner elements: the real order was outer-capture -> inner-capture -> inner-bubble -> outer-bubble β€” capture genuinely goes root-to-target first, then bubble genuinely goes target-to-root.
  • A precise answer names that addEventListener's default is bubble-phase (capture false) β€” most application code never explicitly deals with the capture phase, but understanding it correctly explains constructs like event delegation and why stopPropagation() called during capture can prevent a target's own bubble-phase handler from ever running.

Clarifying questions expected:

  • None β€” this is a definitional/technical question; producing the exact real firing order (not just naming the two phases) is the strong signal.

Code / implementation expected: Yes β€” reproducing the real firing order with listeners on both phases is the most convincing, concrete demonstration.

  • 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 Β· 7 sections

Run the code

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

JavaScriptReal firing order for nested capture- and bubble-phase listeners, reproduced with a dispatched event

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 25 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 do Array.some and Array.every do?

Continue