r/programming • • Mar 26 '14

JavaScript Equality Table

http://dorey.github.io/JavaScript-Equality-Table/
809 Upvotes

331 comments sorted by

View all comments

4

u/webbitor Mar 26 '14 edited Mar 26 '14

I code JS every day, and this held a few surprises for me. Anyone have a good explanation of why if() is different from the ==true column?

if("0"){/*executes*/}
if("0" == true){/*does not*/}

10

u/wtf_apostrophe Mar 26 '14

In the first expression, "0" is a non-empty string, so is 'truthy'. In the second expression, both operands are converted to numbers because the second operand is a bool, so it becomes 0 == 1, which is false.

→ More replies (1)