Equality
Equality in JavaScript is several operations: loose ==, strict ===, Object.is, and user-defined comparisons. They disagree about NaN, -0, and whether "1" equals 1.
Detailed Explanation
Asking "are these the same?" looks like one question. The language answers it four different ways, and interviews love that fact.
Strict equality (===) compares type and value. Different types are never equal. Two numbers are equal if they are the same IEEE-754 value, except that NaN === NaN is false and +0 === -0 is true. Strings compare code units. Objects compare by reference: two literals { a: 1 } are not equal.
Loose equality (==) coerces first, then compares. The rules are long. null == undefined is true, and neither equals 0 or "". A string and a number become numbers. A boolean becomes a number. An object becomes a primitive via ToPrimitive (usually valueOf then toString). This is how [] == false becomes true and [] == ![] becomes true. You do not want to memorise every pair. You want to stop using == except for == null, which is the idiomatic "is null or undefined" check.
Object.is(a, b) is SameValue. It is like === except Object.is(NaN, NaN) is true and Object.is(+0, -0) is false. Use it when those two exceptions matter, for example in a Map key discussion or a React state comparison.
Relational operators (<, >, <=, >=) coerce with ToPrimitive and then compare as numbers unless both sides are strings, in which case they compare UTF-16 order. "10" > "9" is false. [10] > [9] becomes "10" > "9" and is also false.
Objects have no built-in structural equality. JSON.stringify is a poor substitute (key order, undefined, cycles). Write a comparison for the shape you own, or compare ids.
What it costs you: bugs that only show for 0, "", null, and NaN. if (value == false) is a trap. if (!value) is a different trap. Prefer === and spell out the cases you mean: value === 0, value == null, Number.isNaN(value).
Reach for === by default. Reach for == null when missing-or-null is one case. Reach for Object.is when NaN or signed zero must be distinguished. Never reach for == with mixed types on purpose.
Code Examples
console.log(1 === 1); // => true
console.log(1 === "1"); // => false
console.log(1 == "1"); // => true
console.log(null == undefined); // => true
console.log(null === undefined); // => false
console.log(null == 0); // => false
console.log(NaN === NaN); // => false
console.log(Object.is(NaN, NaN)); // => true
console.log(Object.is(0, -0)); // => false
console.log(0 === -0); // => trueconsole.log([] == false); // => true
console.log([] == 0); // => true
console.log("" == 0); // => true
console.log([1] == 1); // => true
// Objects: reference, not contents.
console.log({ a: 1 } === { a: 1 }); // => false
const box = { a: 1 };
console.log(box === box); // => truefunction isBlank(value) {
return value == null || value === "";
}
console.log(isBlank(undefined)); // => true
console.log(isBlank(0)); // => falseCommon Gotchas
`NaN` is not `===` to itself
use `Number.isNaN(value)` (not the global `isNaN`, which coerces first).
`==` coerces through surprising paths
`[] == ![]` is `true`. If you did not intend a conversion, you wanted `===`.
`== null` is the exception worth keeping
it is true only for `null` and `undefined`. `== 0` and `== false` are not similarly tidy.
`Object.is` is not "deep equal"
two objects with the same keys are still not `Object.is` equal.