MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/programming/comments/21ezh3/javascript_equality_table/cgco1ju/?context=3
r/programming • u/vz0 • Mar 26 '14
331 comments sorted by
View all comments
4
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)
10
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.
"0"
0 == 1
→ More replies (1)
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?