== vs === in JavaScript
=== (strict equality) returns true only when both values have the same type and the same value. == (loose equality) converts the operands to a common type first, then compares. Use === by default; the one widely accepted use of == is value == null.
1. Strict equality (===)
No conversion happens. If the types differ, the answer is false.
1 === 1 // true
1 === '1' // false: number vs string
null === undefined // false
NaN === NaN // false: NaN is never equal to anything
0 === -0 // true
{} === {} // false: objects compare by reference
2. Loose equality (==)
When the types differ, JavaScript converts before comparing:
nullandundefinedequal each other and nothing else.- Number vs string: the string becomes a number.
- A boolean becomes a number first (
true→1,false→0). - Object vs primitive: the object is converted to a primitive (
valueOf, thentoString).
1 == '1' // true: '1' → 1
0 == '' // true: '' → 0
true == 1 // true: true → 1
true == 'true' // false: true → 1, 'true' → NaN
null == undefined // true
null == 0 // false: null only equals undefined
[1] == 1 // true: [1] → '1' → 1
[] == false // true: [] → '' → 0, false → 0
3. The results interviewers like to ask about
| Expression | Result | Why |
|---|---|---|
0 == '0' and 0 == '' |
true, true |
Both strings become 0 |
'' == '0' |
false |
Same type, different strings. == is not transitive |
[] == ![] |
true |
![] is false → 0; [] → '' → 0 |
null >= 0 |
true |
Relational operators convert null to 0… |
null == 0 |
false |
…but == never converts null to a number |
NaN == NaN |
false |
Use Number.isNaN(x) |
4. The one good use of ==
if (value == null) {
// true for both null and undefined, false for 0, '' and false
}
It’s shorter than value === null || value === undefined, and most lint configs allow it (eqeqeq with the null: 'ignore' option).
5. Object.is and the two cases === gets “wrong”
Object.is(NaN, NaN) // true
Object.is(0, -0) // false
Object.is is what React uses to compare state and dependency arrays. Array.prototype.includes, Map and Set use a close cousin (SameValueZero), which is why [NaN].includes(NaN) is true while [NaN].indexOf(NaN) is -1.
6. != vs !==
The same rules apply in reverse. 1 != '1' is false (they are loosely equal); 1 !== '1' is true.
Choosing: == vs ===
| Situation | Use |
|---|---|
| Almost everything | === / !== |
| “Is this null or undefined?” | value == null |
Compare with NaN |
Number.isNaN(value) |
Distinguish 0 and -0, or match React’s comparison |
Object.is(a, b) |
| Compare objects or arrays by content | A deep-equal function, never == or === |
Follow-up questions to expect
- “Is
==slower than===?” Not meaningfully. When types match they do the same work. The reason to prefer===is predictability. - “How would you compare two objects?” By reference with
===, or by content with a recursive comparison. Practise it in Deep equals. - “Why is
typeof null === 'object'?” A legacy quirk, unrelated to equality, but it’s often asked right after. See null vs undefined vs undeclared.
For the conversion rules behind all of this, read type coercion in JavaScript.
