/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
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 conversion | Converts operands (ToNumber, ToPrimitive) | Never converts |
| null and undefined | Equal to each other, not to anything else | Not equal to each other |
| NaN | Never equal, not even to itself | Never equal, not even to itself |
| Objects and arrays | Equal only if same reference (after conversion when compared to a primitive) | Equal only if same reference |
| Linting | Flagged by ESLint eqeqeq | Recommended default |
When == is acceptable
value == null is a compact test for "null or undefined":
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
Related pages
config · typescripttsconfig moduleResolution: bundler, nodenext, node16moduleResolution tells tsc how to find imports. TypeScript 7 accepts bundler, node16 and nodenext; node10 and classic were removed. Valid pairs with module.errors · typescripterror TS5108: Option 'moduleResolution=node10' has been removedTypeScript 7 removed moduleResolution node and node10. Use bundler for code built by a bundler, or nodenext with module nodenext for code that Node.js runs.glossary · bashWhat is a shebang (#!) line?A shebang is the #! first line of a script that names its interpreter, such as #!/usr/bin/env bash. The kernel reads it when you run the file as a program.