Strict Equality
Strict equality (=== and !==) compares two values without converting either one. If the types differ, the result is false. Same type, same value, same object reference.
Detailed Explanation
=== is the comparison you should reach for first. It does not call ToNumber or ToString. It does not turn true into 1. A number is never strictly equal to a string, even when they look identical in a log.
The algorithm, in the order the spec uses:
- If the types differ, return
false. - If both are
undefined, or both arenull, returntrue. - If both are numbers, return
truewhen they are the same IEEE value.NaNis not equal to anything, includingNaN.+0and-0are equal. - If both are strings, compare UTF-16 code units in order.
- If both are booleans, compare the boolean.
- If both are symbols, compare symbol identity.
- If both are BigInts, compare the integer.
- If both are objects (including arrays and functions), return
trueonly when they are the same reference.
That last rule is the one that bites. [] === [] is false. Two objects you built from the same JSON are not ===. Strict equality does not mean "looks the same". It means "is the same value, or is the same object".
!== is the negation of ===. There is no extra coercion hiding in the !.
=== is not Object.is. The two differences are NaN and signed zero. Object.is(NaN, NaN) is true; NaN === NaN is false. Object.is(+0, -0) is false; +0 === -0 is true. Almost all application code wants ===. Use Object.is when you are implementing something that must treat NaN as equal to itself (a Set of numbers, a memoization key).
switch uses strict equality. case "1": will not match 1. indexOf and includes on arrays use === (with the includes exception that it finds NaN). Map and Set use SameValueZero, which is === except that NaN matches NaN.
What it costs you: almost nothing, which is why it is the default. The remaining mistakes are comparing an object to a primitive (Number(el.value) === 0 after you already have a string) and expecting two separately constructed objects to be ===.
Reach for === in conditions, in tests, and in API contracts. Document when a function uses Object.is or a deep compare instead.
Code Examples
console.log(0 === 0); // => true
console.log(0 === "0"); // => false
console.log(false === 0); // => false
console.log(null === undefined); // => false
console.log(null === null); // => true
console.log("ada" === "ada"); // => true
console.log("ada" === "Ada"); // => falseconst a = { id: 1 };
const b = { id: 1 };
const c = a;
console.log(a === b); // => false
console.log(a === c); // => true
console.log([1] === [1]); // => falseconsole.log(NaN === NaN); // => false
console.log([NaN].includes(NaN)); // => true
console.log([NaN].indexOf(NaN)); // => -1
switch (1) {
case "1":
console.log("loose?");
break;
case 1:
console.log("strict"); // this one
break;
}Common Gotchas
Two equal-looking objects are not `===`
compare ids, or write a structural compare. Do not stringify as a shortcut without thinking about key order.
`NaN === NaN` is `false`
`Number.isNaN(x)` or `Object.is(x, NaN)` is the test. `indexOf` will not find `NaN`; `includes` will.
`+0 === -0` is `true`
if a divide-by-zero sign matters, use `Object.is` or `1 / x`.
`switch` and `===` are the same rule
coerce the discriminant once if input types are messy.