In JavaScript, why is "0" equal to false, but when tested by 'if' it is not false by itself?

nonopolarity picture nonopolarity · Sep 30, 2011 · Viewed 187.7k times · Source

The following shows that "0" is false in Javascript:

>>> "0" == false
true

>>> false == "0"
true

So why does the following print "ha"?

>>> if ("0") console.log("ha")
ha

Answer

Joe picture Joe · Sep 30, 2011

Tables displaying the issue:

truthy if statement

and == truthy comparisons of all object types in javascript

Moral of the story use === strict equality displaying sanity

table generation credit: https://github.com/dorey/JavaScript-Equality-Table