🚀 JavaScript Output Breakdown Here are the results 👇 false true false true false true false true false false 💡 What’s happening here (in simple terms): NaN === NaN → always false NaN is a special value it’s never equal to anything, even itself. null == undefined → true JS loosely treats both as “no value”. null === undefined → false Different types, so strict equality fails. [1] == 1 → true Array gets converted to '1', then to number → 1. [1] === 1 → false No conversion in strict check → different types. [1,2] == '1,2' → true Array becomes string '1,2'. [1,2] === '1,2' → false Again, type mismatch. [] == ![] → true ![] → false, then [] → 0, false → 0 → equal. [] === ![] → false Boolean vs object → not equal. [] === [] → false Different references in memory. ⚡ Takeaway: Most confusion comes from type coercion in == and reference comparison in objects/arrays. 💬 How many did you get right? #JavaScript #Frontend #WebDevelopment #CodingChallenge
JavaScript Equality Comparison Gotchas
More Relevant Posts
-
**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 Pro-Tips for Cleaner Code 👇 Save this 📌 ✅ Dynamic Object Keys const key = "status"; const obj = { [key]: "active" }; ✅ Array to Object Conversion const obj = { ...['a', 'b', 'c'] }; // { 0: 'a', 1: 'b', 2: 'c' } ✅ Deep Clone (Native) const copy = structuredClone(originalObj); ✅ Quick Integer Conversion const num = +"42"; // Faster than Number() or parseInt() ✅ Flatten Nested Arrays const flat = nestedArr.flat(Infinity); 💡 Modern JS = Less Boilerplate + Better Performance Which of these do you find most useful? Let's discuss below! 🚀 #JavaScript #CodingTips #SoftwareEngineering #Frontend #TypeScript #Developers #WebDevelopment #Coding
To view or add a comment, sign in
-
-
🔍 JavaScript Quirk: == vs === (this will surprise you) Most devs say: 👉 “Always use ===” But do you know WHY? 👇 console.log(0 == false); console.log("" == false); console.log(null == undefined); 💥 Output: true true true Wait… WHAT? 😳 Why this happens? Because == does type coercion 👉 It converts values before comparing Step by step: ✔ false → 0 ✔ "" → 0 So internally: 0 == 0 // true 👉 Special case: null == undefined → true (but NOT equal to anything else) Now compare with === 👇 console.log(0 === false); console.log("" === false); 💥 Output: false false Because === checks: ✔ Value ✔ Type No conversion. No surprises. Now the WEIRDEST one 🤯 console.log([] == false); 💥 Output: true Why? [] → "" → 0 false → 0 👉 0 == 0 Yes… JavaScript really did that 😅 💡 Takeaway: ✔ == tries to be “smart” (and fails) ✔ === is strict and predictable ✔ Use === by default 👉 "Always use ===" is not a rule… It’s survival advice. 🔁 Save this (you’ll forget this later) 💬 Comment "===" if this clicked ❤️ Like for more JS quirks #javascript #frontend #codingtips #webdevelopment #js #developer
To view or add a comment, sign in
-
✨ JavaScript Logic — Handle Broken Images Using error Event Here’s a small but powerful concept that can save your UI from breaking 👇 👉 Problem: Sometimes images fail to load Reasons: Image deleted ❌ Wrong URL ❌ Network issue ❌ Example: <img src="product.png"> 🔹 JavaScript Solution (Using error event) <img src="product.png" onerror="this.onerror=null; this.src='placeholder.png';"/> 🔹 Quick Understanding ✔️ error → event fires when resource fails to load ✔️ Browser triggers it automatically (no manual call) ✔️ Used to handle fallback UI (like dummy image) #JavaScript #Frontend #WebDevelopment #Coding #LearnInPublic
To view or add a comment, sign in
-
🔍 JavaScript Bug You Might Have Seen (null vs undefined) You write this: let a; console.log(a); // ? let b = null; console.log(b); // ? Both look similar… But they are NOT the same. 💥 Output: undefined null Now here’s where it gets confusing 👇 console.log(null == undefined); // ? console.log(null === undefined); // ? 💥 Output: true false Wait… WHAT? 🤯 This happens because of how JavaScript treats them. 📌 What is undefined? 👉 A variable that is declared but NOT assigned a value 📌 What is null? 👉 A value that represents “intentional absence of value” (You explicitly set it) 📌 Then why this? null == undefined // true Because == does loose comparison 👉 It treats them as equal BUT: null === undefined // false Because === checks: ✔ Value ✔ Type 💡 Takeaway: ✔ undefined → JS gives it ✔ null → YOU assign it ✔ == → treats them equal ✔ === → they are different 👉 Use undefined for default/unassigned 👉 Use null when you intentionally want “no value” 🔁 Save this before it confuses you again 💬 Comment “null” if this clicked ❤️ Like for more JavaScript deep dives #javascript #frontend #codingtips #webdevelopment #js #developer
To view or add a comment, sign in
-
🔍 JavaScript Bug You Might Have Seen (setTimeout + loop) You write this code: for (var i = 1; i <= 3; i++) { setTimeout(() => { console.log(i); }, 1000); } You expect: 1 2 3 But you get: 4 4 4 This happens because of closure 📌 What is a Closure? 👉 A closure is a function along with its lexical environment. Not clear? No problem. Here’s a simpler version: 👉 A closure is a function along with the variables it remembers from where it was created. In this case: The function inside setTimeout 👉 remembers the SAME i variable And when it executes: 👉 Loop has already finished → i = 4 So all logs print: 4, 4, 4 How to fix it? ✔ Use let (creates a new variable per iteration) for (let i = 1; i <= 3; i++) { setTimeout(() => { console.log(i); }, 1000); } ✔ Or create a new scope manually for (var i = 1; i <= 3; i++) { (function(i) { setTimeout(() => { console.log(i); }, 1000); })(i); } 💡 Takeaway: ✔ Closures remember variables, not values ✔ var shares the same scope → leads to bugs ✔ let creates a new scope per iteration 👉 If you understand closures, you’ll avoid some very tricky bugs. 🔁 Save this for later 💬 Comment “closure” if this made sense ❤️ Like for more JavaScript deep dives #javascript #frontend #codingtips #webdevelopment #js #developer
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
-
-
I was recently asked a classic JavaScript "gotcha" in an interview: "How do you return an object in an arrow function, and why are parentheses required?" It sounds simple But the "Why" is where most developers get tripped up. The Problem: In JavaScript, {} is ambiguous. It can mean an Object Literal OR a Function Block. By default, the JS engine sees a brace after an arrow => and assumes it's a function block. The Result: const getUser = () => { name: 'Chandhan' }; The engine thinks name: is a label and returns undefined. The Fix: Wrap it in parentheses! ({ ... }) The () forces the engine to treat the contents as an expression, not a statement. ✅ const getUser = () => ({ name: 'Chandhan' }); Small syntax, big difference in how the engine parses your code. #JavaScript #WebDevelopment #CodingTips #Angular #Frontend #InterviewPrep
To view or add a comment, sign in
-
🔍 JavaScript Quirk: this behaves differently than you think This is one of the most confusing parts of JavaScript 👇 const user = { name: "Avinash", greet: function () { console.log(this.name); } }; user.greet(); // ? ✅ Output: "Avinash" Here, this refers to the object calling the function. Now look at this 👇 const greet = user.greet; greet(); // ? 💥 Output: undefined Why? Because this is no longer bound to user. In this case, this refers to the global object (or undefined in strict mode). Now the tricky part 👇 const user = { name: "Avinash", greet: () => { console.log(this.name); } }; user.greet(); // ? 💥 Output: undefined Why? Arrow functions don’t have their own this. They inherit it from their surrounding scope. 🚨 NEVER use this inside arrow functions for object methods. 💡 Takeaway: ✔ this depends on HOW a function is called ✔ Regular functions → dynamic this ✔ Arrow functions → lexical this ✔ Arrow + this in methods = bug waiting to happen 👉 Master this = fewer hidden bugs 🔁 Save this for later 💬 Comment "this" if it clicked ❤️ Like if this helped #javascript #frontend #webdevelopment #codingtips #js #developer
To view or add a comment, sign in
-
"JavaScript is single-threaded… but still handles async tasks?" 🤯 This is where the Event Loop comes in 🔥 Let’s understand it simply 👇 🔹 JavaScript is single-threaded It can do one task at a time using the Call Stack. 🔹 So how does async work? Thanks to: - Web APIs 🌐 - Callback Queue 📥 - Event Loop 🔁 💻 Example: console.log("Start"); setTimeout(() => { console.log("Async Task"); }, 0); console.log("End"); 👉 Output: Start End Async Task 🔹 Why this happens? - "setTimeout" goes to Web APIs - Then moves to Callback Queue - Event Loop waits for Call Stack to be empty - Then executes it 🚀 Pro Tip: Even "setTimeout(..., 0)" is NOT immediate. 💬 Did this surprise you the first time you learned it? 😄 #javascript #webdevelopment #mern #coding #developers
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