Sleepless One@lemmy.ml to Programmer Humor@lemmy.mlEnglish · 1 day agoThe JavaScript type coercion algorithmlemmy.mlimagemessage-square40fedilinkarrow-up1106arrow-down12file-textcross-posted to: programmer_humor@programming.dev
arrow-up1104arrow-down1imageThe JavaScript type coercion algorithmlemmy.mlSleepless One@lemmy.ml to Programmer Humor@lemmy.mlEnglish · 1 day agomessage-square40fedilinkfile-textcross-posted to: programmer_humor@programming.dev
minus-squareTelemachus93@slrpnk.netlinkfedilinkarrow-up12·1 day agoMh, ‘0’ is a nonempty string, so !‘0’ returns false. Then of course !(!‘0’) would return true. I’d absolutely expect this, Python does the same. And the second thing is just JavaScript’s type coercion shenanigans. In Python bool('0') # returns True because of nonempty string bool(int('0')) # returns False because 0 == False Knowing that JavaScript does a lot of implicit type conversions, stuff like that doesn’t strike me as very surprising.
Mh, ‘0’ is a nonempty string, so !‘0’ returns false. Then of course !(!‘0’) would return true. I’d absolutely expect this, Python does the same.
And the second thing is just JavaScript’s type coercion shenanigans. In Python
bool('0') # returns True because of nonempty string bool(int('0')) # returns False because 0 == False
Knowing that JavaScript does a lot of implicit type conversions, stuff like that doesn’t strike me as very surprising.