DEV Community

Cover image for Top 10 weird JavaScript behaviors
The Eagle 🦅
The Eagle 🦅

Posted on

Top 10 weird JavaScript behaviors

NOTE: To get the full potential of this article, I recommend opening the console (F12) and trying out the ones you believe are mind-blowing. Have fun!

1. NaN is a number

   typeof NaN; // "number"
Enter fullscreen mode Exit fullscreen mode

JavaScript treats NaN (Not-a-Number) as a number type, despite its name implying the opposite.

2. Inaccurate Floating-Point Arithmetic

   0.1 + 0.2 === 0.3; // false
Enter fullscreen mode Exit fullscreen mode

Due to floating-point precision errors, simple arithmetic can behave unexpectedly

3. True Equals 1

   true == 1; // true
   true === 1; // false
Enter fullscreen mode Exit fullscreen mode

The loose equality (==) will coerce the boolean true into the number 1, but strict equality (===) respects types.

4. Empty Array and String Concatenation

   [] + []; // ""
   [] + {}; // "[object Object]"
   {} + []; // 0
Enter fullscreen mode Exit fullscreen mode

Adding arrays and objects results in odd outputs like an empty string or zero due to type coercion.

5. Array Length from Crazy Expression

   (![]+[]+![]).length; // 9
Enter fullscreen mode Exit fullscreen mode

JavaScript turns ![] into false, adds an empty array, and coerces the result into a string of length 9.

6. Math with Empty Arrays

   [] == 0; // true
Enter fullscreen mode Exit fullscreen mode

You are comparing an empty array to 0 results in true because JavaScript converts [] to a number (which becomes 0).

7. parseInt and Decimals

   parseInt(0.0000001); // 1
Enter fullscreen mode Exit fullscreen mode

parseInt only reads the integer part of the number, so 0.0000001 gets interpreted as 1.

8. JavaScript Magic Number

   9999999999999999 === 10000000000000000; // true
Enter fullscreen mode Exit fullscreen mode

JavaScript loses precision for huge numbers, treating 9999999999999999 as 10000000000000000.

9. Subtraction & Addition with Booleans

   true - true; // 0
   true + true; // 2
Enter fullscreen mode Exit fullscreen mode

Booleans are treated as 1 (true) and 0 (false) when performing arithmetic, so true - true results in 0. And in true + true both are treated as 1 (true)

10. Adding Different Types

   9 + "1"; // "91"
   9 - "1"; // 8
Enter fullscreen mode Exit fullscreen mode

Adding a number and a string concatenates them as strings while subtracting attempts to convert the string to a number.

Bonus: Empty Array Is True

   [] == false; // true
Enter fullscreen mode Exit fullscreen mode

JavaScript coerces an empty array ([]) into a falsy value when compared with ==, but still considers it truthy in conditions.

Conclusion

Learn JavaScript, and use TypeScript. Your future self will thank you later.

TypeScript does not completely eliminate these problems because they are rooted in JavaScript's runtime behavior. However, TypeScript's type system and compile-time checks reduce the likelihood of encountering many of these issues, especially those related to type coercion and unintended type conversions.

Yes, it takes some time to implement certain features in TypeScript, whereas in JavaScript it takes less time, but the cost is accumulating technical debt and someone will have to pay for it, usually the developer and their team. In TypeScript, at least, you can reduce or eliminate it if you follow good practices and use a design pattern that fits your project's needs.

Meme Js is good but ts is better

Top comments (1)

Collapse
 
ynksdk1654 profile image
ynksdk1654 • Edited

Inaccurate Floating-Point Arithmetic

This is not weird part. This is normal for Python, C++, Java, etc.