JavaScript's NaN Type Confusion Explained

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

  • No alternative text description for this image

Don’t use typeof to check numeric data types. Use Number.isFinite

To view or add a comment, sign in

Explore content categories