Ever clicked a button and noticed that the parent element’s event also runs? That happens because of event bubbling in JavaScript. When an event occurs, it travels up the DOM tree: Button → Parent → Document Sometimes this behavior causes unexpected results in your application. That’s where stopPropagation() helps. It stops the event from moving to parent elements. Example: element.addEventListener("click", (e) => { e.stopPropagation(); }); Now the event will stay on the current element only. Quick reminder: preventDefault() → stops browser default action stopPropagation() → stops event bubbling Understanding small concepts like this helps you write cleaner and more predictable JavaScript code. Follow for more JavaScript concepts explained visually. #javascript #webdevelopment #frontenddeveloper #coding #softwareengineering
Event Bubbling in JavaScript: StopPropagation Explained
More Relevant Posts
-
Day 1 of 30 days of javascript challenge. problem-2667 Problem - Write a function createHelloWorld that returns another function, that returns "Hello World" As this is my first code, I revised my notes on javascript scope. ☑️ Function scope - Any variables declared inside a function body cannot be accessed outside the function body, but global variables can be used inside function body ☑️ Block scope - Any variable declared inside { } cannot be used outside the { } block, although it supports only let and const keyword, var can be used ☑️ Lexical scope - A variable declared outside a function can be accessed inside another function defined after the variable declaration. (The opposite is not true ) This problem uses the concept of closures and higher order functions. Please feel free to discuss where can I improve the code or if you have a different perspective, comment below your views. #javascript #coding #development #motivation #goals #leetcode #webdevelopment
To view or add a comment, sign in
-
-
I ran a small JavaScript experiment today, and it was a good reminder that performance often hides inside simple concepts. I used the same function twice with the same inputs. The first call took noticeable time. The second call returned almost instantly. Nothing changed in the inputs. Nothing changed in the output. The only difference was that the second time, JavaScript didn’t need to do the work again. That’s the beauty of memoization. Instead of recalculating, it remembers the previous result and returns it from cache. What looks like a small optimization in code can make a big difference in how efficiently an application behaves. The deeper I go into JavaScript, the more I realize: the real power is not just in writing code — it’s in understanding how to make code smarter. #JavaScript #WebDevelopment #FrontendDevelopment #Memoization #Closures
To view or add a comment, sign in
-
-
JavaScript Practice – Day by Day Improvement Today I practiced a JavaScript logic problem “Array Chunking.” The goal was to split an array into smaller subarrays (chunks) of a given size. Concepts & Methods Used: • for loop for iteration • Array.slice() method to extract subarrays • Incrementing index by chunk size (i += n) for efficient traversal This approach helps break a large array into manageable chunks and improves understanding of array manipulation and problem-solving in JavaScript. I’m focusing on improving my JavaScript logic day by day through consistent practice. 💻 #JavaScript #ProblemSolving #CodingPractice #WebDevelopment #FrontendDevelopment
To view or add a comment, sign in
-
-
JavaScript can be surprisingly logical… and surprisingly weird at the same time. 😄 Take a look at these three lines: console.log(true + true); console.log(null + 1); console.log(undefined + 1); Before running them, try guessing the outputs. Here’s what JavaScript actually returns: true + true → 2 null + 1 → 1 undefined + 1 → NaN At first, this felt a little strange to me. But it starts to make sense once you remember how type coercion works in JavaScript. true is treated as 1, so 1 + 1 = 2 null becomes 0, so 0 + 1 = 1 undefined turns into NaN, which leads to NaN Small examples like this are a good reminder that JavaScript quietly converts values behind the scenes. And if you’re not aware of it, the results can feel pretty surprising. The deeper I go into JavaScript, the more I realize that understanding these tiny behaviors makes a huge difference in writing reliable code. Which one caught you off guard the most? #javascript #webdevelopment #frontend #coding #learninginpublic
To view or add a comment, sign in
-
JavaScript objects have a powerful feature called Prototype that enables inheritance. In JavaScript, if an object doesn’t have a specific property or method, the engine automatically looks for it in its prototype. This process continues up the prototype chain until the property is found or the chain ends. Example: function Person(name) { this.name = name; } Person.prototype.greet = function () { console.log("Hello, my name is " + this.name); }; const john = new Person("John"); john.greet(); Even though john doesn’t directly have the greet() method, it can still access it through Person.prototype. This mechanism allows JavaScript objects to share methods and inherit behavior efficiently. Understanding prototypes helps you better understand how inheritance and object behavior work behind the scenes in JavaScript. Follow for more JavaScript concepts explained visually. #javascript #webdevelopment #frontenddeveloper #coding #learninginpublic #100DaysOfCode
To view or add a comment, sign in
-
-
JavaScript Closures — made simple 💡 Closures sound complex… but they’re actually simple once you get the idea. A closure is when a function remembers variables from its outer scope even after the outer function has finished executing. Think of it like this: An inner function carries a “backpack” of variables and never forgets them. How it works: 1. Outer function creates a variable 2. Inner function uses that variable 3. Outer function returns the inner function 4. Inner function still has access to that variable Why closures are powerful: • Data privacy (encapsulation) • Maintain state between function calls • Used in callbacks, event handlers, React hooks • Foundation for advanced JavaScript concepts Real-world uses: • Counters • Private variables • One-time execution functions • Custom hooks & memoization One-line takeaway: A closure = function with a memory of its lexical scope If you understand closures, you’re moving from basics to real JavaScript thinking. What concept in JavaScript took you the longest to understand? #JavaScript #Closures #WebDevelopment #Frontend #CodingConcepts #LearnJavaScript #Programming #DeveloperLife
To view or add a comment, sign in
-
-
JavaScript Closures — made simple 💡 Closures sound complex… but they’re actually simple once you get the idea. A closure is when a function remembers variables from its outer scope even after the outer function has finished executing. Think of it like this: An inner function carries a “backpack” of variables and never forgets them. How it works: 1. Outer function creates a variable 2. Inner function uses that variable 3. Outer function returns the inner function 4. Inner function still has access to that variable Why closures are powerful: • Data privacy (encapsulation) • Maintain state between function calls • Used in callbacks, event handlers, React hooks • Foundation for advanced JavaScript concepts Real-world uses: • Counters • Private variables • One-time execution functions • Custom hooks & memoization One-line takeaway: A closure = function with a memory of its lexical scope If you understand closures, you’re moving from basics to real JavaScript thinking. What concept in JavaScript took you the longest to understand? #JavaScript #Closures #WebDevelopment #Frontend #CodingConcepts #LearnJavaScript #Programming #DeveloperLife
To view or add a comment, sign in
-
-
⚠️ The Danger of Splicing Arrays in a Loop "While solving the 'Move Zeroes' challenge today, I encountered a classic JavaScript pitfall: Modifying an array's length while iterating over it. The Mistake: Using .splice() inside a for loop. When you remove an element, the next one shifts left, and your loop index skips it! The Lesson: Always adjust your index or, even better, use the Two-Pointer technique. It's not just safer; it's much more performant for large datasets. Small bugs teach the biggest lessons! 🚀" "Did you know? JavaScript (ES6) allows you to swap array elements in a single line using destructuring : [a, b] = [b, a]. Clean, readable, and efficient!" Feel free to check out my progress and solutions on my LeetCode profile: I've shared my LeetCode profile link in the first comment below! #JavaScript #CodingTips #WebDevelopment #ProblemSolving #LeetCode #AngularDeveloper
To view or add a comment, sign in
-
-
🔍 A small JavaScript detail that can cause unexpected bugs: Object key ordering Many developers assume object keys are always returned in insertion order, but JavaScript actually follows a specific ordering rule when you iterate over object properties (Object.keys, Object.entries, for...in). The order is: • Integer index keys → sorted in ascending order • String keys → insertion order • Symbol keys → insertion order (not included in Object.keys) This is one of the reasons why using Object as a map can sometimes lead to unexpected iteration behavior when numeric keys are involved. If key order matters, Map is usually the more predictable choice since it preserves insertion order for all key types. Small language details like this are easy to overlook, but they often explain those subtle bugs you run into during debugging. #JavaScript #SoftwareEngineering #Frontend
To view or add a comment, sign in
-
-
🚨 JavaScript Hoisting – Something Most Developers Still Misunderstand Most developers say: 👉 “JavaScript moves variables to the top of the scope.” But that’s not actually what happens. Let’s test this 👇 console.log(a); var a = 10; Output: undefined Now try this: console.log(b); let b = 20; Output: ReferenceError: Cannot access 'b' before initialization 💡 Why the difference? Both var and let are hoisted. But the real difference is initialization timing. ✔ var is hoisted and initialized with undefined during the creation phase. ✔ let and const are hoisted but stay inside the Temporal Dead Zone (TDZ) until the line where they are declared. That’s why accessing them before declaration throws an error. 👉 So technically: JavaScript doesn’t “move variables to the top”. Instead, the JavaScript engine allocates memory for declarations during the creation phase of the execution context. Small detail. But it explains a lot of confusing bugs. 🔥 Understanding this deeply helps when debugging closures, scope issues, and async code. #javascript #frontend #webdevelopment #reactjs #coding #softwareengineering
To view or add a comment, sign in
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