Your product could be here, in front of thousands of JavaScript developers. Sponsor this spot →
JavaScript Quiz

Why is false == '0' true in JavaScript?

Do you know what will be the output of this code?

JavaScript
console.log(false == '0');
Show answer and explanation

The answer is true.

In this situation, the abstract equality comparison algorithm is used, which is also called the type-coercing equality check. What it does is converting the values as long as possible, until they match in type and can be compared strictly.

In this case, the following steps are performed:

// 1st step
false == '0'

// 2nd step
Number(false) == '0' -> 0 == '0'

// 3rd step
0 == '0' -> 0 == Number('0')

// 4th step
0 == 0 -> 0 === 0 -> true
Take the full quiz Next question →

More Type Coercion questions

  1. Why does ('b' + 'a' + + 'a' + 'a') return "banana"?
  2. Why does 1 + "2" + "2" return "122"?
  3. Why is 3 > 2 > 1 false in JavaScript?
  4. What do true == [] and true == ![] return?
  5. Why does [] + {} return "[object Object]"?

Related topics