Skip to main content

[] == 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:

javascript
[] == false

Step 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

javascript
false0

Now the expression looks like this:

javascript
[] == 0

Step 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 []:

javascript
[].toString(); // ""

So:

javascript
[]"" // empty string

Now we have:

javascript
"" == 0

Step 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.

javascript
Number("")0

So:

javascript
0 == 0

Result:

javascript
true

Summary by steps

StepExpressionWhat happened
1[] == falsefalse → 0
2[] == 0[] → ""
3"" == 0"" → 0
40 == 0true

Why this is dangerous

This logic makes == unpredictable. Here are more examples:

javascript
[] == ![] // true '' == 0 // true '0' == 0 // true null == undefined // true [] == '' // true [0] == 0 // true [1] == 1 // true [2] == 2 // true

How to avoid confusion

Use strict comparison ===, which does not perform implicit type conversion.

javascript
[] === false // false "" === 0 // false null === undefined // false

In short

ExpressionWhat JS doesResult
[] == false[] → "" → 0, false → 0true
[] === falseCompares without coercionfalse

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.