JavaScript Equality, Explained Like a Human

When to use ===, when == helps, and the gotchas to remember. - 19 Sept 2025

2 min read

views

Illustration for a JavaScript equality explainer

Why equality in JS feels confusing

JavaScript has two equality operators that look similar but behave differently. Most confusion comes from not knowing when the language will convert types for you.

πŸ”‘ The Two Operators

  • === (Strict Equality) β†’ compares value + type, no type conversion.
  • == (Loose Equality) β†’ compares value after type coercion, which makes it weird but sometimes useful.

🚩 Common Myths (and the truth)

Myth 1: == is always bad, never use it.

❌ Wrong.
βœ… Truth: == can be useful if you intentionally want coercion, e.g.

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

It’s dangerous when you don’t expect coercion, but not always β€œbad”.


Myth 2: === is always safer.

❌ Not always.
βœ… Truth: === avoids surprises, but sometimes you want coercion:

if (input == null) {
  // catches both null and undefined
}

Here, == is shorter and clearer than writing:

if (input === null || input === undefined) { ... }

Myth 3: null and undefined are equal to everything with ==.

❌ Nope.
βœ… Truth: they’re only loosely equal to each other:

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

Myth 4: NaN == NaN is true.

❌ Nope.
βœ… Truth: NaN is never equal to anything, not even itself:

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

You must use:

Number.isNaN(NaN)   // true

Myth 5: Objects are compared by value.

❌ Nope.
βœ… Truth: Objects are compared by reference, always:

{} == {}    // false
[] == []    // false

Even if they look identical, different references β†’ not equal.


Myth 6: [] == 0 is false.

❌ Weirdly, it’s true.
βœ… Truth: because coercion happens:

[] == 0
// [] β†’ "" β†’ 0
// so 0 == 0 β†’ true

Myth 7: "0" == false is false.

❌ Actually true.
βœ… Truth: "0" β†’ number 0, and false β†’ number 0, so:

"0" == false   // true

🧠 Rule of Thumb

  • Use === by default for safety.
  • Use == only when you know coercion rules and want them (esp. for null == undefined).
  • Always beware of weird corner cases like [] == ![].