Predictable comparisons
Use === when comparing values in application logic where implicit coercion could hide bugs.
JS Puzzles · Const
JavaScript has three main value-comparison operations: ==, ===, and Object.is(). Their results differ mainly in coercion, NaN handling, and the treatment of signed zero.
The page presents JavaScript's equality comparisons as a table of results for ==, ===, and Object.is(). Loose equality may convert values before comparing; strict equality does not, while Object.is() distinguishes signed zero and treats NaN as equal to itself.
a == b; a === b; Object.is(a, b)Each equality operation returns a boolean.
const values = [
[0, false],
["0", false],
[null, undefined],
[NaN, NaN],
[{}, {}]
];
for (const [a, b] of values) {
console.log({
a,
b,
loose: a == b,
strict: a === b,
sameValue: Object.is(a, b)
});
}Run this snippet to compare loose equality, strict equality, and Object.is for several edge cases.
Use === when comparing values in application logic where implicit coercion could hide bugs.
Use == when intentionally accepting the documented null-or-undefined equivalence.
Use Object.is() when NaN identity or signed-zero behavior matters.
== permits type conversion, so values such as 0 and false can compare equal. === requires matching types and equal values without conversion.
Follow-up: How can NaN be tested reliably? Use Number.isNaN(value) for a type-safe NaN check, or Object.is(value, NaN) for the comparison semantics described by the page.
null == undefined is true, while both values compare unequal to other values under loose equality. This is a special case in the comparison rules.
Follow-up: What does null === undefined return? false, because strict equality rejects operands of different types.
The equality table distinguishes strict equality from Object.is(). Object.is() treats NaN as equal to itself and distinguishes -0 from +0, unlike ===.
Follow-up: What does Object.is(-0, +0) return? false, because Object.is uses SameValue semantics for those numeric values.
Equality (==)Strict equality (===)Object.is()SameValueSameValueZeroIsLooselyEqualIsStrictlyEqual