If ([] == false) is true, why does ([] || true) result in []? - JavaScript

If we look closely at the problem statement, the difference between ([] == false) and ([] || true) is the following −

In the first case, we are using loose conditional checking, allowing type coercion to take over.

While in the second case, we are evaluating [] to its respective Boolean (truthy or falsy) which makes use of the function Boolean() instead of type coercion under the hood.

Let's now unveil the conversions that happen behind the scenes in both cases.

Case 1 - ([] == false)

According to the MDN docs, when two data types are compared using the loose equality operator ==, JavaScript follows specific coercion rules:

First, the boolean value false is converted to a Number using the Number() function:

console.log(Number(false)); // 0
0

So, the condition now becomes:

[] == 0  // Number(false) = 0

Next, the empty array (Object type) is converted to a primitive value by calling its toString() method:

console.log([].toString()); // ""

The condition becomes:

"" == 0

Finally, the empty string is converted to a Number:

console.log(Number("")); // 0
0

This makes the final comparison 0 == 0, which returns true.

console.log([] == false); // true
true

Case 2 - ([] || true)

In this case, the logical OR operator || checks the truthy/falsy value of [] by converting it to a boolean using the built-in Boolean() function:

console.log(Boolean([])); // true
true

Since an empty array is truthy in JavaScript, the logical OR operator returns the first truthy operand, which is [] itself:

console.log([] || true); // []
console.log(typeof ([] || true)); // object
[]
object

Key Difference

The fundamental difference is that == performs type coercion following complex rules, while || simply evaluates truthiness without converting the original value's type.

Conclusion

Understanding JavaScript's type coercion rules is crucial. The == operator converts both operands through multiple steps, while || preserves the original truthy value without type conversion.

Updated on: 2026-03-15T23:18:59+05:30

599 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements