6 ms·
r==b and b==nil but r!=nil seems like the kind of thing that people give JS a lot of crap over.
by esrauch 6y ago
r==b and b==nil but r!=nil seems like the kind of thing that people give JS a lot of crap over.
- mirekrusin 6y agoCan you please give more concrete example that is valid js but is absurd logically?
- camjw 6y agoI think the classic example is [] == ![] evaluates to true. It’s not absurd logically, just the operators act weirdly/non-standardly.
- mirekrusin 6y agoYeah, that one is funny > [] == [] false > [] == ![] true
- mirekrusin 6y agoHowever typescript says: This condition will always return 'false' since the types 'never[]' and 'boolean' have no overlap. ...for `[] == ![]`. Which is ironic because it does return true, not false. However at least it flags an error! I wonder how much of weirdness is wiped out by linters/ts/etc.
- qsort 6y agoHonestly that's more of a weirness of the '==' operator, which is kind of deprecated in modern JS. On the other hand I fully agree that non-strict definitions of equality are bound to generate weird cases around nullish values.
- esrauch 6y agoI don't actually subscribe to the "JS is a bad language" meme (and maybe the meme has died off a bit, partially thanks to typescript), but implicit coercion is one thing that people cite, with behavior exactly like that nontransitive Golang case. Also see this minitalk (which actually gets some things wrong because of confusion on how the terminal interpreter behaves but is still amusing): https://www.destroyallsoftware.com/talks/wat https://www.destroyallsoftware.com/talks/wat
- curryst 6y agoThis talk lists a bunch of them https://www.destroyallsoftware.com/talks/wat https://www.destroyallsoftware.com/talks/wat The first one he talks about is that [] + [] gives you "" (an empty string). I don't know what universe that makes sense in. [] + {} gives you {} back. Meanwhile {} + [] gives you 0. Yes, the order in which you add types that can't logically be added ends up giving you different incoherent answers. {} + {} gives you NaN. One of the other sibling comments said that it was mostly implicit conversions, which I think is the underlying cause of most of these. That's not to totally hate on Javascript, but the language could probably use fewer implicit conversions and more type errors. I struggle to think of a situation in which those outcomes are desirable, and it can cause extremely hard to follow stack traces. A JSON API returns a list of objects instead of a list of strings and now your types are screwed up in hard-to-follow ways.
- esrauch 6y agoSo FYI most of those are misleading. When he's typing into a terminal and it does {}+{} it's interpreting it as and empty code block, and then an unary plus on an empty object, similar to: { }; +{} If you put parentheses around it, or assign it to a value and then print that value then {}+{} isn't empty string.