4 ms·
This 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
by curryst 6y ago
This 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.