Event Bubbling vs Capturing in JavaScript

Day 4/50 – JavaScript Interview Question? Question: What's the difference between Event Bubbling and Event Capturing? Simple Answer: Event propagation has three phases: capturing (from window down to target), target (the element itself), and bubbling (from target back up to window). Bubbling is the default behavior where events propagate upward through parent elements. 🧠 Why it matters in real projects: Understanding event propagation is essential for implementing event delegation, which dramatically improves performance when handling many similar elements (like list items or table rows). It also helps prevent unintended event triggering. 💡 One common mistake: Not knowing when to use stopPropagation() vs stopImmediatePropagation(). The latter also prevents other listeners on the same element from firing, while the former only stops the event from moving to parent/child elements. 📌 Bonus: // Event delegation example - one listener instead of 1000! document.querySelector('ul').addEventListener('click', (e) => { if (e.target.tagName === 'LI') { console.log('List item clicked:', e.target.textContent); } }); #JavaScript #WebDevelopment #Frontend #LearnInPublic #InterviewQuestions #Programming #TechInterviews

To view or add a comment, sign in

Explore content categories