Skip to content

/compare/javascript/double-equals-vs-triple-equals

== vs === in JavaScript: loose vs strict equality

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · v24.20.0updated 2026-10-20
tool
JavaScript
compares
== vs ===

=== (strict equality) is true only when both operands have the same type and value. == (loose equality) first converts the operands to a common type, which produces results such as 0 == '' being true. Use === by default; the one common exception is x == null, which matches both null and undefined.

Side by side

bash
node -e '
const cases = [["0", "\"\""], ["0", "\"0\""], ["\"\"", "\"0\""], ["null", "undefined"], ["NaN", "NaN"], ["[]", "false"], ["[1]", "1"]];
for (const [a, b] of cases) {
  const A = eval(a), B = eval(b);
  console.log(`${a.padEnd(9)} == ${b.padEnd(9)} ${String(A == B).padEnd(5)}   === ${A === B}`);
}'

Output

0         == ""        true    === false
0         == "0"       true    === false
""        == "0"       false   === false
null      == undefined true    === false
NaN       == NaN       false   === false
[]        == false     true    === false
[1]       == 1         true    === false

Criterion by criterion

Criterion== (loose)=== (strict)
Type conversionConverts operands (ToNumber, ToPrimitive)Never converts
null and undefinedEqual to each other, not to anything elseNot equal to each other
NaNNever equal, not even to itselfNever equal, not even to itself
Objects and arraysEqual only if same reference (after conversion when compared to a primitive)Equal only if same reference
LintingFlagged by ESLint eqeqeqRecommended default

When == is acceptable

value == null is a compact test for "null or undefined":

bash
node -e 'for (const v of [null, undefined, 0, "", false]) console.log(JSON.stringify(v) ?? "undefined", v == null)'

Output

null true
undefined true
0 false
"" false
false false

For everything else, compare explicitly with ===. Use Number.isNaN(x) to detect NaN and Object.is(a, b) when -0 and +0 must be told apart.

Sources