5 ms·
I agree JS has a high number of quirks, but like 22 out of these 25 are "implicit type conversion is surprising". It's surprising in almost every language that
by Groxx 3y ago
I agree JS has a high number of quirks, but like 22 out of these 25 are "implicit type conversion is surprising".
It's surprising in almost every language that does it to any degree. JS's is only slightly stranger than, say, PHP. About the only exception is "empty values are falsy", and even that surprises people pretty frequently, and can be used to construct highly confusing snippets of behavior.
- marcosdumay 3y agoMost are not quircks of implicit type conversion, they happen because of the "no errors, at any cost" phylosophy of the language. 1 + "five" is an error in PHP, not some random correct-looking value.
- Groxx 3y agoIn recent PHPs, yes, as they've gotten a lot more strict. On older ones though, it's not an error: https://onlinephp.io?s=s7EvyCjg5UpNzshXMFTQVlBKyyxLVbIGAA%2C%2C&v=5.6.40 https://onlinephp.io?s=s7EvyCjg5UpNzshXMFTQVlBKyyxLVbIGAA%2C... Even in the previous major version (7) it's only a warning. I agree that philosophy is likely the main cause of decisions like this, but JS is hardly unique in having it.
- chrisjj 3y ago> "no errors, at any cost" Meaning of course "no errors reported, at any cost". Programming language language has redefined "error" - to substantial cost to programmers.
- bennyelv 3y agoWhen your system gets to a certain size, this is absolutely fatal. Please halt and throw a massive exception, shrugging and carrying on makes bugs impossible to track down and the whole system unpredictable. It’s a philosophy that might be acceptable for a simple-ish frontend, but when if you try and build something significant it’s just awful. Source: I inherited responsibility for a > 5m LOC system written in JavaScript. The problems aren’t all JavaScript’s fault, but it does play a major part in it.
- chrisjj 3y ago> Please halt and throw a massive exception +1 A case I'm struggling with recently is Fastmail mail search. Given a valid query which it cannot handle, rather than show an error report, it does its "best" and delivers a false result. With potentially great cost to the unwitting user. Is there even a warning on the doc page?? No.
- paulddraper 3y ago> 1 + "five" Forget JavaScript, that's not even an error in Java.
- marcosdumay 3y agoYes, Java has this one strong inconsistency on its type system. The interesting thing is that the addition operator has a not completely predictable behavior exactly because the language designers insisted that custom operators reduce the language predictability. But anyway, it's much less bad in Java because you can force it to err by declaring what type of thing you want it to return.
- white_dragon88 3y ago[dead]
- tedunangst 3y agoLua has a feel very much like javascript, but is much less conversion happy. It helps that there's separate operators for addition and concatenation, so you won't get 1 + 2 = 12 unless you ask for it. Sometimes it seems javascript has a mind of its own, and turns all my numbers into strings when I'm not looking, so I have to sprinkle x = x | 0 throughout the code, like hanging garlic.
- pleoxy 3y agoYou can convert to a number with a + before your variable. const y = +x + 12; Will be number addition even if x is a string.
- WorldMaker 3y agoOr via the Number constructor: const y = Number(x) + 12 Depending on your audience, that might be clearer in intent than `+x`.
- WorldMaker 3y agoPHP also uses different operators for addition and concatenation, but I feel that it has just as much, if not more, implicit conversion weirdness as JS. It seems to me like those two things are orthogonal. (Not that JS wouldn't necessarily be nicer with a separate concatenation operator.)