DEV Community

mayank sagar
mayank sagar

Posted on

⚠️ JavaScript Equality Is Lying to You

⚠️ JavaScript Equality Is Lying to You

(Not because of ===… but because of ==)

Most developers know this rule:

“Always use === instead of ==”

But few know WHY == behaves the way it does 🤯
Let’s expose the weird parts.


🔍 Fact #1: == Follows a Hidden Algorithm (Not Logic)

When you write:

[] == false // true 😵

JavaScript secretly does:

  1. [] → ""

  2. "" → 0

  3. false → 0

0 == 0 // true

This is Abstract Equality Comparison.
Most devs never read it.
But JS always follows it.


🔍 Fact #2: null Is Special (Very Special)

null == undefined // true
null == 0 // false
null == false // false

Why?

null only loosely equals undefined and nothing else.

This one breaks assumptions in condition checks:

if (value == null) {
// catches null AND undefined
}

Many libraries rely on this intentionally.


🔍 Fact #3: NaN Hates Everyone (Even Itself)

NaN === NaN // false
NaN == NaN // false

Only way to detect it:

Number.isNaN(NaN) // true

If you’ve ever compared calculations directly…
you’ve already met this bug 🐛


🔍 Fact #4: === Is Fast Because It Refuses to Think

3 === "3" // false

No conversion.
No negotiation.
No surprises.

This makes ===:

More predictable

Easier to debug

Safer in large codebases

That’s why linters scream when you use ==.


🔍 Fact #5: 3.0 === 3 Works (But 0.1 + 0.2 === 0.3 Doesn’t)

3.0 === 3 // true
0.1 + 0.2 === 0.3 // false

Why?

JavaScript has one number type

Integers are exact

Decimals are approximations (IEEE-754)

Not a JS bug.
It’s how computers count.


🧠 Senior-Level Rule of Thumb

✔ Use === everywhere
✔ Use == only when you understand the coercion rule
❌ Never rely on “JS will figure it out”


🎯 Interview-Ready Summary

== // value comparison + coercion
=== // value + type comparison

JavaScript is not weird.
It’s extremely precise about very strange rules.

If this taught you something new, hit 👍
If it surprised you, comment “🤯”
Let’s expose JavaScript’s hidden contracts.

JavaScript #WebDevelopment #MERN #Frontend #CodingTips #DeveloperLife #JSFacts

Top comments (0)