JavaScript Reference ยท Practical Pages
Equality comparison: ==, === and Object.is()
Concept page comparing JavaScript's three equality algorithms: loose ==, strict ===, and Object.is(), with NaN, -0 and object cases and when to use each one.
JavaScript has three ways to ask "are these the same?", and they give different answers in a few cases. Strict equality (===) compares without converting types. Loose equality (==) converts types first, following a fixed set of rules. Object.is() is like === but treats NaN as equal to itself and tells 0 from -0. A fourth algorithm, "SameValueZero", is used inside includes(), Map and Set.
The practical advice is short: use === and !== by default, reach for Object.is() when NaN or -0 matter, and use == null only as a deliberate shorthand.
Strict equality (===)
Two values are strictly equal when they have the same type and the same value. Different types are never strictly equal. Objects (including arrays and functions) are equal only when they are the very same object. The two exceptions to "same value is equal" are NaN (never equal to anything, itself included) and the fact that 0 === -0 is true.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Loose equality (==)
If both operands have the same type, == behaves exactly like ===. If the types differ, == converts them:
nullandundefinedare loosely equal to each other and to nothing else.- A number and a string: the string is converted to a number.
- A BigInt and a string or number: compared by mathematical value.
- A boolean: it is converted to a number (
truebecomes1,falsebecomes0) and the comparison starts again. This is why"1" == trueistruebut"yes" == trueisfalse. - An object compared with a primitive: the object is converted to a primitive (
valueOf()/toString()/Symbol.toPrimitive) and the comparison starts again. - Two objects are loosely equal only when they are the same object, exactly as with
===.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
The relation is not transitive: 0 == "" and 0 == "0" are both true, but "" == "0" is false. That is the main reason to avoid == in general code.
Object.is()
Object.is(a, b) is the "SameValue" algorithm. It behaves like === with exactly two differences:
Object.is(NaN, NaN)istrue.Object.is(0, -0)isfalse.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Note that String(-0) is "0", so the printed table cannot show the difference; 1 / -0 is -Infinity, which can. [NaN].includes(NaN) is true because includes() uses SameValueZero (like Object.is() but with 0 equal to -0), while indexOf() uses ===.
Quick comparison
| Case | == |
=== |
Object.is() |
|---|---|---|---|
1 and "1" |
true |
false |
false |
null and undefined |
true |
false |
false |
NaN and NaN |
false |
false |
true |
0 and -0 |
true |
true |
false |
| two equal-looking objects | false |
false |
false |
Usage notes
- Default to
===. It has no hidden conversions, and linters can enforce it (theeqeqeqrule). x == nullis a compact test for "nullorundefined", and many teams allow it.x ?? fallbackandx?.yoften read better.- Objects and arrays need a custom comparison. Compare their fields, or compare
JSON.stringifyoutput only for simple, ordered data. - Never compare with
NaNusing an operator. UseNumber.isNaN(x). -0is rare, but it appears in results such as-1 * 0andMath.round(-0.4). Check it withObject.is(x, -0).- Switch statements use
===, socase "1"does not match the number1. - Sorting and sets:
SetandMapuse SameValueZero, sonew Set([NaN, NaN]).sizeis1.
Browser support
All three comparisons are supported everywhere. Object.is() is part of ES2015.