4 ms·
Found out the hard way with Javascript I have to be careful about just doing "if(blah)" to represent what I thought was shorthand for "if(blah !== undefined &&
by cableshaft 4y ago
Found out the hard way with Javascript I have to be careful about just doing "if(blah)" to represent what I thought was shorthand for "if(blah !== undefined && blah !== null)", when I had a weird bug where the logic was being skipped, and discovered that, oh, that also means "if(blah !== 0)"! The value was sometimes 0 and that was also causing it to evaluate to false, when I expected that to be true.
Now to be safe I'm always explicit when checking for undefined and null values, and also try to limit how I use them.
- masklinn 4y ago> that also means "if(blah !== 0)"! The empty string ("") is also falsy. So is `false`, obviously. And the bigint 0. And NaN. Here is the complete truth table: https://262.ecma-international.org/#sec-toboolean https://262.ecma-international.org/#sec-toboolean FWIW undefined and null are loosely equal to one another, it's one of the few cases where loose equality is legitimate. As in if (blah != null) will exclude null and undefined, and only those two[0]. That's step 2 and 3 of IsLooselyEqual: https://262.ecma-international.org/#sec-islooselyequal https://262.ecma-international.org/#sec-islooselyequal [0] and Document.all but if you're dealing with that you're beyond the reach of god
- DonHopkins 4y agohttps://www.reddit.com/r/ProgrammerHumor/comments/225i15/proof_that_brendan_eich_never_really_cared_about/ https://www.reddit.com/r/ProgrammerHumor/comments/225i15/pro... "Proof that Brendan Eich never really cared about equality." https://i.imgur.com/j7u6KbR.png https://i.imgur.com/j7u6KbR.png
- masklinn 4y agoIf we're doing thunderdome, JS doesn't beat C++: https://howardhinnant.github.io/smf.jpg https://howardhinnant.github.io/smf.jpg
- Dylan16807 4y agoBy the time you're declaring copy and move constructors you're much deeper than equality checks.
- DonHopkins 4y ago