Repaso
EspaƱol

What does 0 == "" evaluate to in JavaScript, and 0 === ""?

Options

  1. false, false
  2. true, false (Correct option)
  3. true, true
  4. false, true

Answer

true, false

Why

== (loose equality) first converts both values to a common type (type coercion). The empty string becomes the number 0, and 0 == 0 is true.

=== (strict equality) compares type AND value with no conversion: a number is never equal to a string, so false.

Coercion rules are full of surprises, so always use === and !==.

Remember

Always use === in JavaScript.

Key terms

Practice this question in Repaso