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”

JavaScript treats NaN (Not-a-Numbe…


This content originally appeared on DEV Community and was authored by The Eagle 🦅

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"

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

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

3. True Equals 1

   true == 1; // true
   true === 1; // false

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

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

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

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

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

8. JavaScript Magic Number

   9999999999999999 === 10000000000000000; // true

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

9. Subtraction & Addition with Booleans

   true - true; // 0
   true + true; // 2

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

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

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


This content originally appeared on DEV Community and was authored by The Eagle 🦅


Print Share Comment Cite Upload Translate Updates
APA

The Eagle 🦅 | Sciencx (2024-09-28T17:44:09+00:00) Top 10 weird JavaScript behaviors. Retrieved from https://www.scien.cx/2024/09/28/top-10-weird-javascript-behaviors/

MLA
" » Top 10 weird JavaScript behaviors." The Eagle 🦅 | Sciencx - Saturday September 28, 2024, https://www.scien.cx/2024/09/28/top-10-weird-javascript-behaviors/
HARVARD
The Eagle 🦅 | Sciencx Saturday September 28, 2024 » Top 10 weird JavaScript behaviors., viewed ,<https://www.scien.cx/2024/09/28/top-10-weird-javascript-behaviors/>
VANCOUVER
The Eagle 🦅 | Sciencx - » Top 10 weird JavaScript behaviors. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/28/top-10-weird-javascript-behaviors/
CHICAGO
" » Top 10 weird JavaScript behaviors." The Eagle 🦅 | Sciencx - Accessed . https://www.scien.cx/2024/09/28/top-10-weird-javascript-behaviors/
IEEE
" » Top 10 weird JavaScript behaviors." The Eagle 🦅 | Sciencx [Online]. Available: https://www.scien.cx/2024/09/28/top-10-weird-javascript-behaviors/. [Accessed: ]
rf:citation
» Top 10 weird JavaScript behaviors | The Eagle 🦅 | Sciencx | https://www.scien.cx/2024/09/28/top-10-weird-javascript-behaviors/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.