Skip to content
Tutorials Center
My learning

Topics

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.

JS Strict equality

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:

  1. null and undefined are loosely equal to each other and to nothing else.
  2. A number and a string: the string is converted to a number.
  3. A BigInt and a string or number: compared by mathematical value.
  4. A boolean: it is converted to a number (true becomes 1, false becomes 0) and the comparison starts again. This is why "1" == true is true but "yes" == true is false.
  5. An object compared with a primitive: the object is converted to a primitive (valueOf() / toString() / Symbol.toPrimitive) and the comparison starts again.
  6. Two objects are loosely equal only when they are the same object, exactly as with ===.
JS Loose equality surprises

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) is true.
  • Object.is(0, -0) is false.
JS Comparing === with Object.is()

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 (the eqeqeq rule).
  • x == null is a compact test for "null or undefined", and many teams allow it. x ?? fallback and x?.y often read better.
  • Objects and arrays need a custom comparison. Compare their fields, or compare JSON.stringify output only for simple, ordered data.
  • Never compare with NaN using an operator. Use Number.isNaN(x).
  • -0 is rare, but it appears in results such as -1 * 0 and Math.round(-0.4). Check it with Object.is(x, -0).
  • Switch statements use ===, so case "1" does not match the number 1.
  • Sorting and sets: Set and Map use SameValueZero, so new Set([NaN, NaN]).size is 1.

Browser support

All three comparisons are supported everywhere. Object.is() is part of ES2015.