Stop using || for your Boolean defaults! 🛑 Ever passed false to a function, only to have your code treat it as true? This is a classic JavaScript "falsy" trap. Because false is a falsy value, the logical OR operator (||) skips right over it and grabs your default value instead. The Fix: Use the Nullish Coalescing operator (??). It’s specifically designed to respect false and 0 while still providing a fallback for null or undefined. Check out the snippet below to see the difference! 👇 Have you made the switch to ?? yet, or do you still find yourself reaching for || ? Follow Wisdom Lamodot for more on #JavaScript #WebDevelopment #CodingTips #ReactJS
Avoid Falsy Traps with Nullish Coalescing Operator ??
More Relevant Posts
-
updated solution based on feedback by Yevhen Serbyniuk how "this" works in JavaScript. the confusion: this is not consistent. it changes depending on how your function is called. arrow functions work differently than regular functions. callbacks lose context. it's a mess. why it matters: this is everywhere in JavaScript. event handlers, callbacks, class methods. if you don't understand it, you're debugging ghosts. the real issue: regular functions get this from their caller (dynamic binding). arrow functions capture this lexically from where they're defined (static binding). this fundamental difference breaks code constantly. when it breaks hardest: callbacks inside methods. event listeners in classes. promises with then(). async operations. all the places where context gets lost or wrong. the solution: understand what this should be in your context. use arrow functions if you want parent's this. use regular functions with bind() if you want explicit control. the rule: regular functions = this from caller. arrow functions = this from definition scope. know which you need. #javascript #typescript #webdevelopment #buildinpublic #reactjs
To view or add a comment, sign in
-
-
🚀 JavaScript Output Challenge #4 (Trap Level) If you think you understand JavaScript deeply… this one will test you 👇 🧠 Question: (Check the code in the images) ⚠️ Rules: Don’t run the code Think step by step Focus on execution order 🤔 Try to answer: What will be the exact output? Why does it happen? What changes if we replace var with let? 💬 Drop your answers in the comments Let’s see how many get this right 👀 🔥 Concepts involved: Event Loop Microtask vs Macrotask Closures Scope (var vs let) 📌 I’ll share the detailed explanation soon #javascript #webdevelopment #frontend #codingchallenge #reactjs #nodejs
To view or add a comment, sign in
-
-
One thing that used to confuse me in JavaScript: Why do both == and === even exist? Coming from other languages, it felt unnecessary. Turns out, they solve two different problems. == tries to be flexible and converts values: 0 == "0" → true === is strict, no conversion: 0 === "0" → false So it’s not random. It’s just two different rules: flexible vs exact. Most of the time, you just want exact. #javascript #webdevelopment
To view or add a comment, sign in
-
Unpopular opinion: You don’t need to learn 5 frameworks to get started in tech. One solid foundation in HTML, CSS, and JavaScript is more powerful than jumping between tools. You can learn framework like react.js once you have a solid foundation of html , css and javascript Depth > speed. #coding #front-end #react.js
To view or add a comment, sign in
-
🎡 JavaScript Event Loop — Quick Challenge Most developers get this wrong 👀 🧪 What will be the output of this code? (Check the image 👇) 👉 Drop your answer in the comments before scrolling. ⏳ Think first... . . . ✅ Answer 1. Start 4. End 3. Promise.then (Microtask) 2. setTimeout (Macrotask) 🔍 Simple Explanation JavaScript runs code in this order: 1️⃣ First → Normal (synchronous) code 2️⃣ Then → All Promises (Microtasks) 3️⃣ Finally → setTimeout (Macrotasks) 👉 Even if setTimeout is 0, it still runs later. 🧠 Takeaway Promise.then → runs sooner setTimeout → runs later Simple rule, but super useful in real projects. 💬 What was your answer? #JavaScript #EventLoop #Frontend #WebDevelopment #CodingTips
To view or add a comment, sign in
-
-
Understanding the "this" keyword in JavaScript 🔍 The value of "this" depends on how a function is called — not where it is defined. Here are the key cases: • Global scope → "this" refers to the global object (window in browser) • Object method → "this" refers to the object calling the method • Constructor function → "this" refers to the new instance created • Event handler → "this" refers to the element that triggered the event Mastering "this" helps in writing better object-oriented and reusable code. Still practicing with different examples to understand it deeply 💻 #javascript #webdevelopment #frontend #learninginpublic #mern
To view or add a comment, sign in
-
-
**AVOID JAVASCRIPT'S QUIET BUGS:** **Don't use ==, use ===** --- 🔺 **THE PROBLEM: == COERCION** ```js console.log([] == false); // prints true! 😅 ``` **The behind-the-scenes journey:** ``` [] ↓ (empty array) "" ↓ (string) 0 ↓ (number) false ↓ (boolean) 0 ``` Wait, why is that true? JavaScript silently converts types, leading to unexpected results. **This is confusing behavior!** --- 🔺 **THE FIX: STRICT EQUALITY** ```js console.log([] === false); // prints false! 🎉 ``` Uses strict comparison without hidden type conversions. **Predictable and safe.** --- 🔺 **NO HIDDEN CONVERSION!** --- **MAKING YOUR CODE PREDICTABLE: ALWAYS USE ===** Have you encountered confusing JavaScript type coercion? Share your experience! 👇 #javascript #webdevelopment #frontend #coding #developers #mern #learning
To view or add a comment, sign in
-
-
🚨 JavaScript Gotcha: When 0 Actually Matters One of the most subtle bugs in JavaScript comes from using the logical OR (||) for default values. const timeout = userTimeout || 3000; Looks fine… until userTimeout = 0. 👉 JavaScript treats 0 as falsy, so instead of respecting your value, it silently replaces it with 3000. 💥 Result? Unexpected behavior. ✅ The Fix: Use Nullish Coalescing (??) const timeout = userTimeout ?? 3000; This only falls back when the value is null or undefined — not when it’s 0. 💡 When does 0 actually matter? ⏱️ Timeouts & delays → 0 can mean run immediately 📊 Counters & stats → 0 is a valid value, not “missing” 💰 Pricing / discounts → Free (0) ≠ undefined 🎚️ Sliders / configs → Minimum values often start at 0 🧠 Rule of thumb: Use || when you want to catch all falsy values (0, "", false, etc.) Use ?? when you only want to catch missing values (null, undefined) ⚡ Small operator. Big difference. Cleaner logic. #reactjs,#nodejs #JavaScript #WebDevelopment #CleanCode #Frontend #ProgrammingTips #DevTips #CodeQuality #SoftwareEngineering
To view or add a comment, sign in
-
-
most developers don't understand closures and it breaks their loops. classic problem: everyone uses let now so this fixes itself but the problem is still there, just hidden. here's why it happens: - when you use var > it's function-scoped not block-scoped. - by the time the timeout runs, the loop is done and i is 3. - all three callbacks reference the fix with let: let is block-scoped so each iteration gets its own i . but here's what you should actually know: - this isn't a JavaScript problem. this is a closure problem. - every function closes over the variables around it. - understanding that changes everything about how you debug async code, event listeners, callbacks. stop memorizing the fix. understand why it happens. #javascript #typescript #webdevelopment #buildinpublic #reactjs
To view or add a comment, sign in
-
-
🚀 Day 21 – Hoisting Explained (JavaScript) Ever logged a variable before declaring it and got undefined instead of an error? 🤔 That’s hoisting in action! 💡 JavaScript moves declarations to the top of their scope before execution — but there’s a catch… 🔹 var is hoisted (but only declared) 👉 Initialized as undefined 🔹 let & const are hoisted too… BUT ❌ They live in the Temporal Dead Zone (TDZ) 👉 Accessing them early throws an error 🔹 Functions behave differently ✅ Function declarations → fully hoisted ❌ Function expressions → NOT hoisted the same way ⚠️ Why this matters? Hoisting can silently introduce bugs if you’re not careful. 🔥 Pro Tip (Angular Devs): ✔ Prefer let & const ✔ Avoid relying on hoisting ✔ Write clean, predictable code 🧠 Once you understand hoisting, debugging weird undefined issues becomes MUCH easier! 💬 Have you ever been confused by hoisting? Drop your experience below 👇 #JavaScript #Angular #Frontend #WebDevelopment #100DaysOfCode
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