Difficulty: Beginner
What is the difference between == and === in JavaScript, and when, if ever, should you use ==?
=== (strict equality) returns true only if both operands have the same type and the same value, with no conversion. == (loose equality) first converts the operands to a common type using the Abstract Equality rules and then compares, which produces surprising results like '' == 0 being true. Use === by default; the one widely accepted use of == is value == null to check for both null and undefined.
The loose equality algorithm in the spec boils down to a few rules:
===.null == undefined is true, and neither equals anything else (null == 0 is false).'42' == 42 is true).true becomes 1), so true == '1' is true but true == 'yes' is false.valueOf/toString (ToPrimitive), then compared again. [1] == 1 is true because [1] becomes '1' and then 1.| Expression | == | === |
|---|---|---|
0 == '' | true | false |
0 == '0' | true | false |
'' == '0' | false | false |
false == '0' | true | false |
null == undefined | true | false |
null == 0 | false | false |
[] == false | true | false |
NaN == NaN | false | false |
Notice that == is not transitive: 0 == '' and 0 == '0', yet '' != '0'. That alone is a good reason to avoid it.
Neither operator compares structure. { a: 1 } === { a: 1 } and [1, 2] == [1, 2] are both false because they are different objects. Two variables pointing at the same object are equal. For value comparison you need a deep equality helper, or for simple data JSON.stringify (which is order-sensitive and has limits).
=== has two quirks: NaN === NaN is false, and +0 === -0 is true. Object.is fixes both: Object.is(NaN, NaN) is true and Object.is(0, -0) is false. React uses Object.is to decide whether state or dependencies changed. To test for NaN, use Number.isNaN(x).
eqeqeq rule is enabled in most style guides, often with the { null: 'ignore' } option to allow x == null.Number(input) === 0.localStorage always return strings, which is exactly where == hides bugs.console.log('42' == 42, '42' === 42); // true false
console.log(0 == '', 0 == '0', '' == '0'); // true true false
console.log(false == '0', [] == false); // true true
console.log(null == undefined, null === undefined); // true false
console.log(null == 0, null >= 0); // false true (relational ops convert null to 0)
== converts types before comparing, which leads to non-transitive results. The last line shows null behaving differently under == and >=.
function isMissing(v) {
return v == null; // true for null and undefined only
}
console.log(isMissing(null), isMissing(undefined), isMissing(0), isMissing(''));
console.log(NaN === NaN, Object.is(NaN, NaN)); // false true
console.log(0 === -0, Object.is(0, -0)); // true false
const a = { id: 1 };
const b = { id: 1 };
console.log(a === b, a === a); // false true
v == null is a concise check for both nullish values. Object.is handles NaN and signed zero, and objects are only equal to themselves.
Strict Equality, Loose Equality, Type Coercion, Object.is, Abstract Equality Algorithm