JavaScript uses the === operator, called strict equality, so that a comparison checks both the value and the type with no hidden conversion. Without it you would have only ==, which converts operands first and can report 0 == '0' as true. In everyday code, === is the safe default and == is the exception.
This guide focuses on practical use: the rules === follows for each type, bugs it prevents in real code, and how to enforce it.
=== in conditions, switch and arraysThe === operator follows a short, fixed set of rules:
false.NaN is never equal to anything.null, undefined and symbols are equal only to the identical value.Rule 5 matters most: === does not compare the contents of objects.
Form fields, query strings and JSON often deliver strings where you expect numbers. Loose equality hides that mismatch and strict equality exposes it early.
The strict version forces you to convert once, in one visible place. That is easier to read, test and debug than coercion buried in every comparison.
=== in conditions, switch and arraysStrict equality is used elsewhere in the language without you writing it:
switch statement compares its cases with strict equality, so case '1' does not match the number 1.Array.prototype.indexOf and includes use strict equality (with includes treating NaN as equal to itself).Set and Map keys use the SameValueZero algorithm, close to strict equality.Knowing this explains why a lookup with the wrong type quietly fails.
Make the choice automatic rather than relying on memory:
"eqeqeq": ["error", "always"], or ["error", "always", { "null": "ignore" }] to still allow == null.== unless a comment explains why it is intentional.Prettier does not change operators, so linting is the right layer.
JavaScript has four equality algorithms. Knowing them helps you choose the right one:
| Algorithm | Used by | NaN equals NaN | 0 equals -0 |
|---|---|---|---|
| IsLooselyEqual | == | No | Yes |
| IsStrictlyEqual | ===, switch | No | Yes |
| SameValueZero | includes, Map, Set | Yes | Yes |
| SameValue | Object.is | Yes | No |
Most code needs only the first two. Reach for Object.is when you must tell 0 from -0, or when you write a library that compares arbitrary values, as React does for state updates.
typeof results with ==. Write typeof value === 'string'.array === [] to detect an empty array. Use array.length === 0.Number() and check Number.isNaN first.NaN === NaN works. Use Number.isNaN(value).===. Two Date objects are different references, so compare a.getTime() === b.getTime().== and === in one sentence?== converts types before comparing, while === compares value and type and never converts.
=== compare objects by content?No. It compares references, so two separate objects with identical fields are not equal.
== ever?Rarely. The accepted case is value == null to match both null and undefined.
===?!==, the strict inequality operator. It is true when the values or the types differ.
JavaScript uses === so comparisons are exact: same type and same value, no coercion. It prevents bugs from strings and numbers mixing, behaves the same inside switch and includes, and is easy to enforce with ESLint. Use === and !== by default.