[] == false ??
Short answer
Because when comparing with ==,
JavaScript automatically coerces both sides to a number,
and ultimately compares 0 == 0.
Step-by-step explanation
Let's take:
[] == falseStep 1. == triggers type coercion
With loose comparison (==), JS tries to coerce the types so the comparison is correct.
The algorithm is described in the ECMAScript standard §7.2.14 as follows:
If one of the operands is
boolean, it is coerced to a number.
Step 2. Convert false → to a number
false → 0Now the expression looks like this:
[] == 0Step 3. JS sees that the left side is an object (array), and the right side is a primitive
If one of the operands is an object, it is first coerced to a primitive
(via valueOf() → toString()).
For the array []:
[].toString(); // ""So:
[] → "" // empty stringNow we have:
"" == 0Step 4. Comparing "" == 0
According to the same algorithm:
If one operand is a string and the other is a number → the string is converted to a number.
Number("") → 0So:
0 == 0Result:
trueSummary by steps
| Step | Expression | What happened |
|---|---|---|
| 1 | [] == false | false → 0 |
| 2 | [] == 0 | [] → "" |
| 3 | "" == 0 | "" → 0 |
| 4 | 0 == 0 | true |
Why this is dangerous
This logic makes == unpredictable.
Here are more examples:
[] == ![] // true
'' == 0 // true
'0' == 0 // true
null == undefined // true
[] == '' // true
[0] == 0 // true
[1] == 1 // true
[2] == 2 // trueHow to avoid confusion
Use strict comparison ===,
which does not perform implicit type conversion.
[] === false // false
"" === 0 // false
null === undefined // falseIn short
| Expression | What JS does | Result |
|---|---|---|
[] == false | [] → "" → 0, false → 0 | true |
[] === false | Compares without coercion | false |
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.