Javascript is a weird language! 🤯 Take a guess before scrolling... . . . . ANSWERS: ✅ [] == ![] → true (wait, what?!) ✅ typeof null → "object" (it's not though) ✅ NaN === NaN → false (NaN doesn't even equal itself) ✅ '3' + 1 → "31" (string concatenation) ✅ '3' - 1 → 2 (numeric subtraction) WHY THIS HAPPENS: 1. The Empty Array Paradox Arrays are truthy, but when compared using loose equality, both sides get converted to numbers - empty arrays become 0, and the negation of a truthy value also becomes 0 2. The typeof null Bug This is a long-standing bug in JavaScript that was never fixed for backward compatibility, It's been wrong since day one. 3. NaN is Special This behavior follows the IEEE 754 standard for floating-point numbers. Use isNaN() to check for it instead. 4. The + Operator Does Double Duty When one operand is a string, + concatenates. The - operator only does math, so it forces type conversion to numbers. THE LESSON? Always use strict equality (===) instead of loose equality (==). It checks both value AND type without coercion. JavaScript is powerful, but it demands respect and understanding of its quirks. Have you been bitten by any of these? Drop your favorite JavaScript WTF moment in the comments 👇 P.S. If JavaScript were a person, it would be that friend who says "trust me" right before something goes horribly wrong. But we love it anyway. ❤️ #JavaScript #WebDevelopment
JavaScript Quirks: Understanding the Weird and Witty Language
More Relevant Posts
-
This one small difference has caused real bugs in real code: == vs ===. == is loose equality. JavaScript converts types before comparing. So 0 == false is true. '5' == 5 is true. null == undefined is true. === is strict equality. No type conversion. '5' === 5 is false. 0 === false is false. The rule I follow: always use ===. The only time == makes sense is when you explicitly want type coercion — and that's rare. I learned about truthy and falsy values alongside this: 0, '', null, undefined, NaN, and false are all falsy in JavaScript. Everything else is truthy. This matters in conditionals: if (user) {} // true if user is defined and not null/undefined/empty Understanding implicit conversion is an interview topic. It's also something that causes actual production bugs when developers don't think carefully about it. Do you always use ===, or do you have cases where you reach for ==?
To view or add a comment, sign in
-
-
JavaScript is powerful, but also weird sometimes Some things that still surprise developers 👇 1. [] + [] → "" Reason: Arrays are converted to strings → empty string + empty string 2. [] + {} → "[object Object]" Reason: Object gets converted to string 3. {} + [] → 0 Reason: {} is treated as a block, not an object 4. typeof null → "object" Reason: Historical bug in JavaScript (never fixed) 5. NaN === NaN → false Reason: NaN is not equal to anything, even itself 6. 0.1 + 0.2 === 0.3 → false Reason: Floating point precision issue 7. false == '0' → true Reason: Type coercion converts both to number 8. [] == false → true Reason: Both become 0 after coercion 9. '' == 0 → true Reason: Empty string converts to 0 10. null == undefined → true Reason: Special equality rule in JS => Always prefer === over == to avoid surprises
To view or add a comment, sign in
-
JavaScript's most confusing line — and it's only 15 characters Look at this carefully: typeof("6" / "abc") // → 'number' "6" / "abc" // → NaN Wait. The result is NaN — Not a Number. But typeof says it's a number? Yes. That's not a bug. That's JavaScript working exactly as designed. Here's what's actually happening: 🔹 JS tries to divide "6" by "abc" 🔹 It coerces "6" into the number 6 first 🔹 "abc" can't be coerced — becomes NaN 🔹 6 / NaN = NaN And here's the part that breaks everyone's brain: NaN is technically of type number in JavaScript. NaN = Not a Number. typeof NaN = "number". ✔️ NaN means the operation failed to produce a valid number ✔️ But the type system still classifies it as numeric type ✔️ It's an IEEE 754 floating point standard decision — not just JS ✔️ NaN === NaN returns false — NaN is not equal to itself Pro tip: Never check for NaN using === Use Number.isNaN(value) instead. It's the only reliable way. JavaScript doesn't always fail loudly. Sometimes it just quietly returns NaN and keeps moving. That silent failure is what makes JS bugs so hard to trace in real apps. Did you know NaN was typeof number before seeing this? #JavaScript #WebDevelopment #Programming #SoftwareEngineering #Frontend #JS #TechLearning
To view or add a comment, sign in
-
-
JavaScript coercion is not a feature. It's a horror movie. 😵💫 You write something simple like: [] + [] JavaScript responds: "Cool. That's an empty string." 👍 You try: [] + {} Now it says: "[object Object]" 😌 You push your luck: {} + [] And JavaScript hits you with: "0. Take it or leave it." At this point, you're not coding anymore. You're in a situationship with a programming language. Everything works... until it suddenly doesn't. And no one can explain why. The worst part? You can't even be mad. Because technically... it's behaving "correctly." Just not the way any sane developer would expect. As someone who writes React every day, I've made peace with JavaScript's mood swings. Type coercion is that one colleague who's technically right but socially chaotic. So here's the real question: If you can explain JavaScript coercion without opening a single browser tab... I genuinely respect you. 🫡 Drop your wildest JavaScript "wat" moment in the comments. Everybody have one 👇
To view or add a comment, sign in
-
-
"If you’re not using "map", "filter", and "reduce" in JavaScript… you're probably writing more code than needed." 😅 These 3 array methods can level up your code instantly 👇 🔹 map() 👉 Transforms each element of an array 👉 Returns a new array 💻 Example: const nums = [1, 2, 3]; const doubled = nums.map(n => n * 2); // [2, 4, 6] 🔹 filter() 👉 Filters elements based on a condition 👉 Returns a new array 💻 Example: const nums = [1, 2, 3, 4]; const even = nums.filter(n => n % 2 === 0); // [2, 4] 🔹 reduce() 👉 Reduces array to a single value 👉 Very powerful (but often misunderstood) 💻 Example: const nums = [1, 2, 3, 4]; const sum = nums.reduce((acc, curr) => acc + curr, 0); // 10 🚀 Pro Tip: Use "map" for transformation, "filter" for selection, and "reduce" for everything else. 💬 Which one do you use the most in your projects? #javascript #webdevelopment #mern #coding #developers
To view or add a comment, sign in
-
💡 Still confused about call(), apply(), and bind() in JavaScript? Let’s fix that in 60 seconds. If you've ever struggled with how this works in JavaScript… you're not alone. These three methods are your secret weapons to take control of it. 👉 Here’s the simplest way to understand them: 🔹 call() Invokes the function immediately Pass arguments one by one fn.call(thisArg, arg1, arg2) 🔹 apply() Also invokes immediately But takes arguments as an array fn.apply(thisArg, [args]) 🔹 bind() Does NOT run immediately Returns a new function you can call later const newFn = fn.bind(thisArg) 🧠 Think of it like this: call → “Run now, here are the args” apply → “Run now, here’s a list of args” bind → “Save this for later with this context” ✨ Why it matters: Mastering these helps you: ✔ Control this like a pro ✔ Reuse methods across objects ✔ Avoid common bugs in callbacks & event handlers 🔥 Pro tip: In modern JS, apply() is less common thanks to the spread operator: fn.call(thisArg, ...args) 📌 If you're preparing for interviews or leveling up your JS fundamentals — this trio is a must-know. 💬 Which one do you use the most: call, apply, or bind? #JavaScript #WebDevelopment #Frontend #CodingTips #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Closures in JavaScript — A Function with Memory Closures are one of the most powerful concepts in JavaScript that every developer should master. 👉 A closure allows an inner function to access variables from its outer function’s scope — even after the outer function has finished execution. 🔍 Key Points: - Access outer scope variables anytime - Preserve data without using global variables - Enable private variables (data encapsulation) - Maintain state across function calls 💡 Example Use Cases: - 🔒 Data Privacy (encapsulation) - 🔄 State Management - 🎯 Event Handlers & Callbacks - ⚛️ Custom Hooks in React 📌 Takeaway: A closure is simply “a function that remembers its lexical environment.” Understanding closures deeply will level up your JavaScript skills and help you write cleaner, more efficient code. 💬 What’s one JavaScript concept that took you time to master?
To view or add a comment, sign in
-
-
🧠 == vs === in JavaScript (One Small Difference That Causes Big Bugs) One of the first things I learned in JavaScript was: 👉 Always use === instead of == But I didn’t fully understand why until I saw how they actually behave. Here’s a simple breakdown 👇 🔹 == (Loose Equality) == compares values after type conversion (type coercion). Example: 0 == "0" // true false == 0 // true JavaScript tries to convert values to the same type before comparing. This can lead to unexpected results. 🔹 === (Strict Equality) === compares both value and type. Example: 0 === "0" // false false === 0 // false No type conversion happens here. 🔹 Why this matters Using == can introduce subtle bugs because of automatic type coercion. Using === makes your code: ✅ more predictable ✅ easier to debug ✅ less error-prone 💡 One thing I’ve learned: Small JavaScript concepts like this can have a big impact on code reliability. Curious to hear from other developers 👇 Do you ever use ==, or do you always stick with ===? #javascript #frontenddevelopment #webdevelopment #reactjs #softwareengineering #developers
To view or add a comment, sign in
-
-
Do you know this weird JavaScript fact? 😯 👉 NaN === NaN → false Yes… NOT even equal to itself. Wait, what? 🤯 Here’s why: NaN stands for “Not a Number”, but in JavaScript it represents an invalid or undefined numeric result. According to the IEEE standard, NaN is never equal to anything — not even itself. So this will always be false: NaN === NaN ✔️ Then how do you check it? Use: Number.isNaN(value) 💡 Real-world gotcha: If you're comparing values and expecting NaN to match, your condition will silently fail… and debugging becomes painful. The lesson? JavaScript isn’t weird… until it is 😄 Have you ever faced this bug? 👇 #JavaScript #WebDevelopment #Frontend #Coding #nan #mern
To view or add a comment, sign in
-
-
JavaScript dates are finally fixed. Stop struggling with the broken "Date" object. 1. The problem: A 30-year-old headache The current `Date` object in JavaScript is famous for being difficult. It is full of traps that create bugs: - if you change a date, it often changes the original one by mistake. - adding 2 months or 15 days requires complex logic. - handling different locations is almost impossible without big libraries (like Moment.js). The old code: // Adding 7 days is not intuitive let d = new Date(); d.setDate(d.getDate() + 7); // Careful: the original 'd' is now changed! 2. The solution: the temporal API Temporal is a modern way to handle dates and times. It is much more powerful and easier to read. The new code: // Simple, clear, and safe const today = Temporal.Now.plainDateISO(); const nextWeek = today.add({ days: 7 }); // 'today' stays the same! Why this is a game-changer: - an object never changes. When you add time, it creates a new one. This prevents bugs. - you can add or subtract years, months, and days with one simple command: `.add()`. - iIf you only need a "Date" (without the time), there is a specific tool for that (`PlainDate`). No more useless "00:00:00" timestamps. Temporal is the future of JavaScript. Right now, it works in Chrome and Firefox. Edge and Safari have it in "preview" mode. It is not yet available in Opera. Are you ready to play with this new API in your own playground?
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