𝗨𝗻𝗱𝗲𝗿𝘀𝘁𝗮𝗻𝗱𝗶𝗻𝗴 𝗘𝘃𝗲𝗻𝘁 𝗣𝗿𝗼𝗽𝗮𝗴𝗮𝘁𝗶𝗼𝗻 𝗶𝗻 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁- Ever clicked a button inside a card and wondered what actually happens behind the scenes? That is event propagation at work. 𝗘𝘃𝗲𝗻𝘁 𝗽𝗿𝗼𝗽𝗮𝗴𝗮𝘁𝗶𝗼𝗻 describes how a single event travels through the DOM hierarchy. 𝗥𝗲𝗮𝗹-𝗪𝗼𝗿𝗹𝗱 𝗘𝘅𝗮𝗺𝗽𝗹𝗲: Imagine a parent card component containing multiple child buttons. When you click a button: • The button receives the event first • The event then moves to the parent card • It continues upward through the DOM 𝐓𝐡𝐢𝐬 𝐦𝐨𝐯𝐞𝐦𝐞𝐧𝐭 𝐨𝐟 𝐞𝐯𝐞𝐧𝐭𝐬 𝐢𝐬 𝐩𝐫𝐨𝐩𝐚𝐠𝐚𝐭𝐢𝐨𝐧. 𝗧𝗵𝗲𝗿𝗲 𝗮𝗿𝗲 𝟯 𝗽𝗵𝗮𝘀𝗲𝘀: 1. 𝗖𝗮𝗽𝘁𝘂𝗿𝗶𝗻𝗴 𝗣𝗵𝗮𝘀𝗲 (Top --> Down): The event travels from the root element toward the target. 2. 𝗧𝗮𝗿𝗴𝗲𝘁 𝗣𝗵𝗮𝘀𝗲: The event reaches the exact element that was clicked. 3. 𝗕𝘂𝗯𝗯𝗹𝗶𝗻𝗴 𝗣𝗵𝗮𝘀𝗲 (Bottom --> Up): The event bubbles back up to the root. Use 𝐞𝐯𝐞𝐧𝐭.𝐬𝐭𝐨𝐩𝐏𝐫𝐨𝐩𝐚𝐠𝐚𝐭𝐢𝐨𝐧() to prevent parent handlers from executing. #JavaScript #FrontendDevelopment #ReactJS #WebDevelopment #Programming #TechLearning #SoftwareEngineering #SystemDesign #DOM #FrontendInterview
Event Propagation in JavaScript Explained
More Relevant Posts
-
Here’s something interesting about JavaScript’s single-threaded nature. If you run a long synchronous task (like a 10-second while loop), the UI freezes. During that time, if you click a button multiple times, nothing appears to happen. But when the loop finishes, all the clicks suddenly fire. Why? Because JavaScript is blocked — but your Operating System isn’t. Here’s what actually happens: Your mouse click sends a hardware interrupt to the OS. The OS records it in its input buffer. The browser receives the event and places it into the Macrotask Queue. The Event Loop cannot process the queue until the Call Stack is empty. When the synchronous task completes, the Event Loop finally runs again and processes all queued click events. JavaScript is single-threaded. The system underneath it is not. Understanding this explains many “weird” UI behaviors. #JavaScript #WebDevelopment #EventLoop #Programming
To view or add a comment, sign in
-
-
Mastering Conditional Logic in JavaScript 🚀 | if-else, Ternary & Switch Explained Clearly Understanding conditional logic is fundamental to controlling program flow in JavaScript. In this tutorial, I explore how decision-making structures shape dynamic applications. 🔹 if-else statements – Ideal for evaluating range-based conditions, such as determining voting eligibility based on age. 🔹 Ternary operator – A concise and efficient alternative for simple true/false decisions, helping write cleaner and more compact code. 🔹 Switch statements – Best suited for handling multiple fixed cases like menu selections or predefined operations, improving readability and structure. Each method serves a specific purpose. While they may overlap in functionality, choosing the right conditional structure enhances clarity, maintainability, and performance of your code. Strong fundamentals in control flow lead to stronger application logic. #JavaScript #WebDevelopment #Programming #Coding #FrontendDevelopment #LearnToCode
To view or add a comment, sign in
-
15 minutes wasted… and it wasn’t even JavaScript’s fault. I was convinced something was wrong with map(). My array looked fine. But this kept printing in my console: [undefined, undefined, undefined] Here’s what I wrote: const doubled = numbers.map((num) => { num * 2; }); Turns out… I forgot one word: 𝗿𝗲𝘁𝘂𝗿𝗻 When you use { } in map(), you need an explicit 𝗿𝗲𝘁𝘂𝗿𝗻. That tiny detail cost me time. Now I always check one thing: Am I returning something or just running code? A small mistake, easy to miss, yet still humbling. What’s a JS bug that made you question your sanity? 👇 #JavaScript #FrontendDevelopment #WebDevelopment #CodingTips #JuniorDevelopers #LearnToCode #Programming #CodeDebugging #DeveloperLife #CleanCode
To view or add a comment, sign in
-
-
⏱️ setTimeout() vs setInterval() — Know the Difference Both setTimeout() and setInterval() are widely used for scheduling tasks in JavaScript, but they behave differently and are suited for different use cases. Key differences: setTimeout() executes once after a specified delay setInterval() executes repeatedly at fixed intervals setTimeout() offers better control and accuracy setInterval() can drift over time if tasks take longer to execute Best practice: Prefer setTimeout() for controlled or recursive scheduling Use setInterval() carefully when timing precision matters Understanding these nuances helps avoid unexpected behavior and performance issues in real-world applications. #JavaScript #WebDevelopment #FrontendDevelopment #Programming #DeveloperTips #SoftwareEngineering
To view or add a comment, sign in
-
-
A few days ago I asked myself a simple question: Why does the JavaScript Event Loop split work into microtasks and macrotasks? The short answer is priority and state consistency. Microtasks (Promises, queueMicrotask, MutationObserver) always run before the engine moves to the next macrotask. Macrotasks include setTimeout, setInterval, DOM events, network callbacks, and script execution. The idea is simple: Microtasks usually complete critical logic — resolving values, updating state, finishing async flows — things that should be finalized before rendering, handling the next event, or running timers. Microtasks are designed to finish important work before the system moves forward. Understanding this makes it much easier to reason about async behavior, rendering, and race conditions in large JavaScript applications. #javascript #frontend #webdevelopment #reactjs #softwareengineering #softwarearchitecture #engineering #asynс #eventloop #programming #coding #frontenddeveloper
To view or add a comment, sign in
-
-
⚡ Stop Breaking Your React Buttons: Choose the Right onClick Syntax Small syntax choices in React can silently hurt performance, readability, and even cause infinite re-renders. Here’s a simple breakdown of the 3 most common onClick patterns 👇 1️⃣ onClick={handleClick} Best default choice ✅ Passes a function reference ✅ Stable → works great with memo and optimized components ✅ Clean and performant ❌ Can’t pass arguments directly 2️⃣ onClick={() => handleClick(id)} Use when you need arguments ✅ Flexible and easy to customize ✅ Perfect for list items, IDs, dynamic data ⚠ Creates a new function on every render ⚠ Avoid overusing in large or memoized lists 3️⃣ onClick={handleClick()} This is a bug, not a feature 🚫 Executes immediately during render 🚫 Not an event handler 🚫 Can cause infinite re-renders 👉 Never use this in JSX ⚠️ Performance Tip Avoid inline arrow functions in render props when working with memoized components or large lists. 💬 Comment “React” if this helped 🔁 Follow me for more React, hooks, and real-world UI tips #ReactJS #WebDevelopment #FrontendDeveloper #Programming #LearningJourney #Developers #FullStack
To view or add a comment, sign in
-
-
One JavaScript concept that finally clicked for me was closures. Not the textbook definition. The practical part. A function remembers the variables around it, even after that outer function is done running. So state can live quietly where you don’t see it, but it’s still there. At first this felt like magic. Then it felt dangerous. Now it feels useful. Most bugs I’ve had with closures weren’t because JS was weird. They were because I forgot what my code was holding onto. #JavaScript #Coding #Programming
To view or add a comment, sign in
-
𝗪𝗲𝗹𝗰𝗼𝗺𝗲 𝘁𝗼 𝗗𝗮𝘆 𝟭𝟵 𝘠𝘰𝘶 𝘵𝘳𝘪𝘨𝘨𝘦𝘳 𝘢𝘯 𝘈𝘗𝘐 𝘳𝘦𝘲𝘶𝘦𝘴𝘵. 𝘉𝘶𝘵 𝘣𝘦𝘧𝘰𝘳𝘦 𝘪𝘵 𝘧𝘪𝘯𝘪𝘴𝘩𝘦𝘴: 𝘛𝘩𝘦 𝘶𝘴𝘦𝘳 𝘯𝘢𝘷𝘪𝘨𝘢𝘵𝘦𝘴 𝘵𝘰 𝘢𝘯𝘰𝘵𝘩𝘦𝘳 𝘱𝘢𝘨𝘦 𝘰𝘳 𝘛𝘩𝘦 𝘯𝘦𝘵𝘸𝘰𝘳𝘬 𝘣𝘦𝘤𝘰𝘮𝘦𝘴 𝘱𝘢𝘪𝘯𝘧𝘶𝘭𝘭𝘺 𝘴𝘭𝘰𝘸 Should that request still continue? In production systems, the answer is often no. 𝙒𝙝𝙖𝙩 𝘼𝙗𝙤𝙧𝙩𝘾𝙤𝙣𝙩𝙧𝙤𝙡𝙡𝙚𝙧 𝙍𝙚𝙖𝙡𝙡𝙮 𝙍𝙚𝙥𝙧𝙚𝙨𝙚𝙣𝙩𝙨 AbortController is not about stopping JavaScript. It is about: -> Controlling the lifetime of asynchronous operations. -> It sends a cancellation signal. If the operation supports it (like fetch): -> The request is terminated -> The promise rejects intentionally You handle the outcome cleanly This prevents undefined behavior and stale updates. #JavaScript #WebDevelopment #FrontendDevelopment #AbortController #APIDesign #SoftwareEngineering #AsyncProgramming #CleanCode #PerformanceOptimization #JSDeveloper #Programming
To view or add a comment, sign in
-
-
I explored the concept of callbacks, which are essential for handling asynchronous operations. 👉 A callback is simply a function passed as an argument to another function, executed after a task completes. This pattern is widely used in JavaScript for: 🔹 Event handling 🔹 Timers (setTimeout) 🔹 Array methods (forEach, map) 🔹 API calls and async operations Example: function greet(name) { console.log("Hello " + name); } function processUser(callback) { callback("Padmaja"); } processUser(greet); Callbacks help JavaScript stay non-blocking and efficient, allowing tasks like user interactions or data fetching to run smoothly. Next, I’m exploring how Promises and Async/Await improve readability and solve callback nesting (“callback hell”). Learning step by step and building stronger foundations every day 🚀 #JavaScript #WebDevelopment #AsyncProgramming #Callbacks #Frontend #Programming #LearningJourney
To view or add a comment, sign in
-
-
Day 11/60: Leveling up the JavaScript Game. 📈 Today wasn't about new syntax—it was about controlling the flow of time in my code. ⏳ Tackled Asynchronous JavaScript head-on: ✅ Promises: Taming the pending state. ✅ Async/Await: Making my code read like a story, not a maze. ✅ Fetch: Reaching out into the internet and bringing data home. We're done with the sandbox examples. Time to plan some real-world apps where this async magic actually matters. Async/Await > Callback Hell. It's not even close. What's the first app you ever built that used an API? Let me know in the comments! 👇 #JavaScript #Coding #Developer #AsyncJS #Programming #60DayChallenge
To view or add a comment, sign in
More from this author
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development