Learn JavaScript
❯ operators.equality

Equality: == vs === in JavaScript

$concept

Strict equality (===) compares type and value with no conversion: 1 === "1" is false, and it is the correct default everywhere. Loose equality (==) coerces first through rules nobody fully memorizes — null == undefined is true (the sanctioned exception), "" == false is true, [] == false is true — producing bugs that read as correct code.

Object.is() refines further: Object.is(NaN, NaN) is true (unlike ===) and Object.is(-0, +0) is false (unlike ===). Team rule: === always, == never (except == null as the deliberate covers-both check), Object.is for NaN/zero edge cases.

$how it works
1

=== compares type + value

1 === "1" is false, 1 === 1 true, {} === {} false (different references) — predictable, no coercion.

2

== coerces unpredictably

"" == false, 0 == false, [] == false are all true — implicit conversions that read as equality but aren't.

3

The null exception

x == null covers null AND undefined in one check — the single legitimate ==, recognized by every linter.

4

Object.is for edges

SameValue semantics: NaN equals NaN, -0 differs from +0 — reach for it in numeric edge-case code.

$example
console.log(1 == "1", 1 === "1");
console.log(null == undefined, null === undefined);
console.log(NaN === NaN, Object.is(NaN, NaN));
Output
true false
true false
false true
1Loose == coerces "1" to 1 (true); strict === sees different types (false).
2The sanctioned exception: == unites them, === separates them.
3=== says NaN differs from itself; Object.is says identical — edge-case precision on demand.
$use cases
Defaulting to === in every comparison (lint eqeqeq enforces it)
Using == null deliberately as the covers-both-absences check
Reaching for Object.is in numeric code with NaN/zero concerns
Reading legacy == as potential coercion bugs during review

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in JavaScript yourself — building beats watching.