3 ms·
Interesting - I view it the opposite way. In JavaScript, you need to know the details of exactly one numeric type: the regular, standard IEEE 754 float that's u
by jkaptur 6y ago
Interesting - I view it the opposite way. In JavaScript, you need to know the details of exactly one numeric type: the regular, standard IEEE 754 float that's used by Java, Python, C++ (generally), and every other programming language under the sun. That's it! There is only one way to do it ;)
These days, if you need huge integers, you can use BigInt, but if you want to use it in an expression that also contains floating point values, then you need to explicitly convert it. Explicit is better than implicit.
- umvi 6y agoI just find it counter intuitive, that's all. The expectation is that high level scripting languages have BigInt enabled by default (since that's what Ruby/Python do). Not so with JS. The expectation is that sorting an array of integers in place: [1,3,2,4,5,7,6,8,9,10].sort() produces an array with the numbers in ascending order (since that's what Ruby/Python do). Not so with JS (try it out). A agree there is a perfectly logical reason for every quirky behavior, but I've been surprised (in a bad way) so many times by JS's counter-intuitive behavior that I now have a mental collection of gotchas where JS deviates from expectation.
- chrisseaton 6y agoConsider this: JavaScript actually has a formal specification. Ruby and Python have no formal specification. I can tell you (with reference to the specification) exactly what a JavaScript program does. JavaScript may seem wacky on the surface, but actually it's all worked out. Nobody can tell you what a Ruby or Python program does. And you think JavaScript is the footgun?
- umvi 6y agoWell yes, in theory if all the developers read JS's formal specification JS would be better. But in practice developers just wing it without reading anything, and only go to read things when getting an unexpected result. And so we get the follow outcome - python behaves as expected in more cases than JS behaves as expected. "Theory is great in theory, but in practice, practice is better"
- jkaptur 6y agoI mean, if you define "whatever Ruby and Python do" to be intuitive, then, sure, every other language is going to have "a long list of weird footguns". JS has many quirks, but I like that the numeric representation is extremely simple and explicit (even compared to Python).
- mschuetz 6y agoThat's a problem with the specification of sort, not with javascript using double by default. But yeah, that sort example is seriously bonkers.
- ficklepickle 6y agoArray.sort converts to UTF-16 code units, or some such weirdness, by default. If you are sorting anything other than strings, you will want to pass in a comparison function. [1,3,2,4,5,7,8,9,10].sort((a,b) => a - b) gives you the behavior one would expect. I suppose you already know that, though. Agreed there are lots of quirks.
- recursive 6y agoExcept it also has bitwise operators which is definitely weird.
- ygra 6y agoAnd those bitwise operators are defined to convert the number into a 32-bit signed integer as part of the process. Which is why foo | 0 is a way of converting a number to an integer (as long as it's not too large ...).