JavaScript is "fine." Everything is "fine." 🫠 If you think you know JS, try explaining these without checking MDN: 🔹 [] + [] → "" 🔹 [] + {} → "[object Object]" 🔹 [] == ![] → true 🔹 "5" - 3 → 2 🔹 "5" + 3 → "53" Why does this happen? It all comes down to Type Coercion. * The + operator favors strings. * The - operator favors numbers. * The == operator is the "Wild West" of implicit conversion. The Fix? 1. Use === (Strict Equality) always. 2. Be explicit with your types (Number(), String()). 3. Don't add arrays to objects. Just... don't. Strong fundamentals separate the "coders" from the "engineers." What’s the most "surprising" JS behavior you’ve encountered? Let's discuss below. #FrontEnd #Coding #JavaScript #WebDev #TechCommunity
JavaScript Type Coercion: Understanding the
More Relevant Posts
-
In the world of JavaScript, "truthy" and "falsy" are more than just quirky terms - they are the source of some of the most persistent bugs in production. In JavaScript, only these eight values evaluate to false when forced into a boolean context: 1. false (The keyword) 2. 0 (The number zero) 3. -0 (Negative zero) 4. 0n (BigInt zero) 5. "" (Empty string) 6. null 7. undefined 8. NaN (Not-a-Number) Anything else is NOT falsy. Here's some common false positives - [] (Empty arrays) {} (Empty objects) " " (A string containing only a space) "false" (The string "false") So if you have an array, don't check if(myArray), instead check if(myArray.length > 0) Mastering these nuances makes your code more resilient and your intent clearer. #JavaScript #WebDevelopment #CodingTips #SoftwareEngineering #Frontend
To view or add a comment, sign in
-
JavaScript Trick You Should Know! Ever thought Object.freeze() makes everything immutable? Think again 👇 const user = Object.freeze({ name: "John", skills: ["JS"] }); user.skills.push("React"); console.log(user.skills); 🤔 What will be the output? 👉 ["JS"] 👉 ["JS", "React"] 👉 Error 👉 undefined ✅ Answer: ["JS", "React"] 💡 Why? Object.freeze() is shallow, not deep. It prevents changes to the top-level object But nested objects/arrays (like skills) are still mutable So this works 👇 user.skills.push("React"); // ✅ Allowed 🔥 Pro Tip If you need full immutability, you must use a deep freeze approach or libraries like: Immer Lodash (cloneDeep + freeze) Custom recursive freeze function #JavaScript #WebDevelopment #Frontend #CodingTips #Developers #JS #Programming #TechInterview
To view or add a comment, sign in
-
-
💥 JavaScript: Where Logic Goes to Die 🤯 Think you understand JavaScript? These mind-bending quirks might change your mind… ⚡ Ever wondered: 👉 Why 9999999999999999 becomes 10000000000000000? 👉 Why 1 < 2 < 3 is true, but 3 > 2 > 1 is false? 👉 Why [] == false is true but [] === false is false? 🧠 Welcome to the world of: ✔️ Type Coercion ✔️ Floating Point Precision ✔️ Weird Comparisons ✔️ Truthy vs Falsy Values 💡 For example (from page 9): 0.1 + 0.2 === 0.3 → ❌ false Because JavaScript uses floating-point math, resulting in 0.30000000000000004 😂 And the classic: 'b' + 'a' + + 'a' + 'a' → “banana” (sort of 😅) 🚀 These aren’t bugs — they’re how JavaScript actually works! 📌 If you're a developer, mastering these quirks will save you from real-world bugs. 💬 Which one shocked you the most? #JavaScript #WebDevelopment #Coding #JS #Frontend #Programming #Developers #CodingLife
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
-
Confused between var, let, and const? Here's all you need to know. 👇 Most JavaScript developers use all three — but few know exactly when and why. Here's a quick breakdown: ⚠️ var → Function-scoped, hoists as undefined, can be re-declared. Avoid it in modern code. 🔁 let → Block-scoped, re-assignable. Use it when values change. 🔒 const → Block-scoped, immutable binding. Your default choice. 💡 TDZ (Temporal Dead Zone) — let and const are hoisted but can't be accessed before their declaration line. That's a feature, not a bug. 💥One rule of thumb: Always start with const. Switch to let only when you need to reassign. Never touch var. Save this for your next code review. 🔖 #JavaScript #WebDevelopment #Frontend #JS #Programming #100DaysOfCode #CodeTips #SoftwareEngineering
To view or add a comment, sign in
-
-
Ever faced a moment in JavaScript where something just didn’t make sense? 😅 I was debugging a small issue and saw this: 1 + "2" → "12" "5" - 2 → 3 [] == ![] → true At first, I thought I messed up somewhere… but nope — it was type coercion doing its thing. Basically, JavaScript tries to be “helpful” by converting values automatically. Sometimes it helps… sometimes it confuses you even more. Here are a few examples I explored 👇 • 1 + "2" → "12" (number becomes string) • "10" - 5 → 5 (string becomes number) • true + 1 → 2 • false + 1 → 1 • null + 1 → 1 • undefined + 1 → NaN • [] + [] → "" • [] + {} → "[object Object]" • {} + [] → 0 (weird one 👀) • [] == false → true • "0" == false → true • null == undefined → true • null === undefined → false What I learned from this 👇 → If there’s a string, + usually concatenates → Other operators (-, *, /) convert to number → == can trick you, === is safer Now whenever I see weird JS behavior… my first thought is: “Is type coercion involved?” 😄 #javascript #webdevelopment #coding #frontend #learning
To view or add a comment, sign in
-
-
🚀 A Simple JavaScript Trick That Stops Unexpected Bugs Imagine a set of Russian nesting dolls. The biggest doll holds a smaller one, which holds an even smaller one, and so on. Each doll can keep a secret that only the doll inside it can see. In JavaScript a closure works the same way. When you create a function inside another function, the inner function remembers the environment of the outer one, even after the outer function has finished running. That hidden environment is the secret stored in the doll. Think of it like this: you have a function that creates a special discount code. The outer function generates the code and stores it in a variable. It then returns a new function that, when called later, gives you the same code without exposing the variable to the rest of the program. The inner function is the tiny doll that still holds the secret code, safe from accidental changes. ✅ Quick mental test: write a function that returns another function which adds a fixed number to any input. The fixed number is set once, then the returned function can be reused. No global variables are needed, and the added value stays private inside the closure. Did this help? Save it for later. Check if your scripts use closures to keep data safe and tidy. #WebDevelopment #LearnToCode #WordPress #CodingTips #TechEducation #WebDesign #JavaScript #Frontend #Programming #Developer #Coding #WebDev #Tech #LearnJS #CodeNewbie
To view or add a comment, sign in
-
Most JS developers will look at this and say: "Easy. It returns the object." Wrong. It returns undefined. And if you don’t know why, your code is a ticking time bomb. 💣 Here is the "Invisible Villain" that’s breaking your production builds: The Culprit: ASI (Automatic Semicolon Insertion) JavaScript tries to be "smart." When it sees a return followed by a new line, it doesn't wait for your object. It says: "Oh, you forgot a semicolon? Let me fix that for you." It transforms your code into: return; Everything after that? Dead code. Ignored. Void. The Fix? Never let your curly braces { lonely on a new line after a return. Keep them on the same line. Stop letting JavaScript "guess" what you mean. Be explicit. Or be prepared to debug for hours. What’s the weirdest JS bug you’ve ever shipped? Let’s hear the horror stories below. 👇 #JavaScript #WebDev #Programming #CodingTips #SoftwareEngineering #Frontend #CleanCode #TechMindset #SohamParakhStyle #CareerGrowth
To view or add a comment, sign in
-
-
🚨 JavaScript Challenge — 90% Get This Wrong No long questions. Just pure JavaScript. 👇 Answer before checking comments. 🧠 𝟭. 𝗪𝗵𝗮𝘁 𝘄𝗶𝗹𝗹 𝘁𝗵𝗶𝘀 𝗹𝗼𝗴? console.log(0.1 + 0.2 === 0.3); Yes or No? And why? ⚡ 𝟮. 𝗣𝗿𝗲𝗱𝗶𝗰𝘁 𝘁𝗵𝗲 𝗼𝘂𝘁𝗽𝘂𝘁 console.log("5" - 3); console.log("5" + 3); Same inputs. Different results. Explain. 🔥 𝟯. 𝗧𝗵𝗶𝘀 𝗼𝗻𝗲 𝗯𝗿𝗲𝗮𝗸𝘀 𝗯𝗿𝗮𝗶𝗻𝘀 console.log([] == ![]); True or False? (No guessing — explain it.) 🧩 𝟰. 𝗢𝘂𝘁𝗽𝘂𝘁? const obj = { a: 1 }; Object.freeze(obj); obj.a = 2; console.log(obj.a); Does it change or not? 🚀 𝟱. 𝗙𝗶𝗻𝗮𝗹 𝘁𝗿𝗮𝗽 console.log(typeof undefined); console.log(typeof null); Why is JavaScript like this? 😄 💬 Be honest — how many did you get 100% correct without Googling? Drop your answers below 👇 Let’s see who actually understands JS fundamentals. I’ll post detailed explanations in the next post. Follow if you don’t want to miss it 🔥 #javascript #frontend #codingchallenge #webdevelopment #techinterview #DAY86
To view or add a comment, sign in
-
🚀 JavaScript Concepts Series – Day 6 / 30 📌 Closures in JavaScript 👀 Let’s Revise the Basics 🧐 A closure is when a function remembers variables from its outer scope even after the outer function has finished execution. 🔹 Key Points • Inner function can access outer variables • Data persists even after function execution • Useful for data privacy and state management 🔹 Example function outer() { let count = 0; return function inner() { count++; console.log(count); }; } const counter = outer(); counter(); // 1 counter(); // 2 💡 Key Insight Closure → Function + its lexical scope Remembers → Outer variables after execution Closures are widely used in callbacks, event handlers, and React hooks. More JavaScript concepts coming soon. 🚀 #javascript #js #webdevelopment #frontenddeveloper #coding #programming #developers #softwaredeveloper #learnjavascript #javascriptdeveloper #codinglife #devcommunity #webdev #reactjs #mernstack #codingjourney #codeeveryday #developerlife #100daysofcode #techlearning
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