JavaScript's NaN Surprise: Why Comparisons Fail

Is JavaScript lying to you? 🤔 𝗡𝗮𝗡 === 𝗡𝗮𝗡 => 𝗳𝗮𝗹𝘀𝗲 The same value is not equal to itself This is not a bug, but design decision. NaN means “Not a Number”, and JavaScript treats it as: “I refuse to compare this to anything.” So this will never work: 𝘃𝗮𝗹𝘂𝗲 === 𝗡𝗮𝗡 The only safe way: 𝗡𝘂𝗺𝗯𝗲𝗿.𝗶𝘀𝗡𝗮𝗡(𝘃𝗮𝗹𝘂𝗲) Why this matters 👇 Your validations silently fail Your calculations look fine… until production Your UI breaks with no errors JavaScript isn’t hard, It’s full of traps. If this surprised you, your code probably has this bug already 😅 #JavaScript #DevLife #Programming #SoftwareEngineering #Frontend #Bugs

To view or add a comment, sign in

Explore content categories