The === operator (strict equality) returns true only when both operands have the same type and the same value. The == operator (loose equality) first converts the operands to a common type and then compares them, so 5 == "5" is true while 5 === "5" is false. In modern JavaScript you should use === by default and keep == for the one deliberate case of checking for null or undefined.
Both operators are defined by the ECMAScript specification, so the behaviour is the same in every browser, Node.js, Deno and Bun.
=== never converts anything. If the types differ, the result is false immediately. If the types match, the values are compared.
For objects, arrays and functions, === compares references, not contents. Two separate objects with identical properties are not strictly equal, which is covered in the section on reference types below.
== applies the abstract equality algorithm. When the operand types differ, one or both sides are converted, usually toward a number. That is why the results can look surprising.
The rules are hard to memorise, and they are not transitive: "" == 0 and 0 == "0" are both true, yet "" == "0" is false. This is the main reason style guides discourage ==.
| Expression | == | === |
|---|---|---|
1 and "1" | true | false |
0 and false | true | false |
"" and false | true | false |
null and undefined | true | false |
NaN and NaN | false | false |
[] and false | true | false |
| two equal-looking objects | false | false |
Neither operator looks inside objects. Both check whether the two operands are the same reference.
To compare contents, compare the fields you care about, or use structuredClone plus a deep-equality helper from your test library. Do not rely on JSON.stringify for equality, because key order and values such as undefined change the result.
There is one widely accepted use: value == null is true for both null and undefined, and for nothing else.
Many ESLint setups allow this through eqeqeq: ["error", "always", { "null": "ignore" }]. If you prefer full strictness, value === null || value === undefined or the nullish operators ?? and ?. express the same idea.
=== and !== everywhere by default.Number(input) === 5, not input == 5.eqeqeq rule so loose comparisons fail in review.NaN, use Number.isNaN(x). For the SameValue rules (distinguishing +0 from -0), use Object.is.The difference is negligible in modern engines. Choose === for correctness and readability, not for speed.
The specification contains a dedicated rule that makes null and undefined loosely equal to each other and to nothing else. Strictly, they are different types, so null === undefined is false.
Yes. Strings are primitives, so "abc" === "abc" is true when the characters are identical. Comparison is case-sensitive and compares UTF-16 code units, so normalise with normalize() if you compare accented text.
Object.is(NaN, NaN) is true and Object.is(0, -0) is false, the opposite of === in both cases. Use it when those edge cases matter.
=== compares type and value with no conversion, while == converts first and produces results that are hard to predict. Prefer === always, allow == null only if your team agrees on it, and remember that both operators compare objects by reference.