JavaScript Quirks: Understanding the Weird and Witty Language

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-challenge

To view or add a comment, sign in

Explore content categories