What does 0 == "" evaluate to in JavaScript, and 0 === ""?
Options
- false, false
- true, false (Correct option)
- true, true
- 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.