JavaScript Equality, Explained Like a Human
When to use ===, when == helps, and the gotchas to remember. - 19 Sept 2025
2 min read

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. fornull == undefined
). - Always beware of weird corner cases like
[] == ![]
.