DEV Community

[Comment from a deleted post]
Collapse
 
lexlohr profile image
Alex Lohr

Let's have a look at the examples that make you hate JavaScript, shall we?

9999999999999999 == 10000000000000000  true
0.1 + 0.2 === 0.3  false // (this website explains https://0.30000000000000004.com )
1.0000000000000001 === 1  true
Enter fullscreen mode Exit fullscreen mode

Now every language that handles numeric values as IEEE754 floating point numbers has the exact same issue. The first one can be fixed by using BigInt:

9999999999999999n == 10000000000000000n // → false
Enter fullscreen mode Exit fullscreen mode

The other examples cannot be easily fixed, but will fail in most other languages either.

typeof NaN == 'number'  true
NaN != NaN  true
Enter fullscreen mode Exit fullscreen mode

You have detected an interesting feature of the language to support mathematical functions. NaN is basically an undefined numeric value, thus compares equal to nothing, not even itself. You can detect it by using isNaN. In other language, you'd probably express this with undefined or null, but that would still compare equal to similar undefined solutions, which could lead to mistakes.

[8, 19, 24, 1, 5].sort()  [1, 19, 24, 5, 8]
Enter fullscreen mode Exit fullscreen mode

Having a look at the documentation shows us that Array.prototype.sort will compare the string values of the items for sorting, but you can easily use a lambda function to compare two items:

[8, 19, 24, 1, 5].sort((x, y) => x - y)  [1, 5, 8, 19, 24]
Enter fullscreen mode Exit fullscreen mode

So actually most of your issues either are not actual issues of the language or can be easily solved by learning more about the language instead of expecting it to work exactly like the languages that you already know.

The only valid point you have is that the weak type system can easily lead to errors - and that is already rectified by TypeScript and similar static type checkers.