JavaScript numbers are always stored as double-precision floating-point numbers. There is no integer data type. Some newbies to JavaScript do not realize that and believe that 125 === 125.0 is false. If you are assuming number to be an integer datatype then that is exactly when mistakes related to this happen. As strange as it may seem, the following evaluates to true in Node.js
125 // one twenty five.
125.0 // same
125 === 125.0 // returns true
Those that are unfamiliar with the way JavaScript works usually mention another unexpected behavior that is easy to explain once you understand the language. Adding 0.1 to 0.2 produces 0.3000000000000000444089209850062616169452667236328125, which means that 0.1 + 0.2 is not equal to 0.3.
console.log((0.1+0.2) === 0.3) // returns false
Unfortunately, the quirks with numbers in JavaScript doesnβt end here. Even though Numbers are floating points, operators that work on integer data types are work here as well.
5 % 2 === 1 // true
5 >> 1 === 2 // true
However you no need to worry while dealing with large numbers because there are plenty of big integer libraries that implement the important mathematical operations on large precision numbers or can try the modern built-in JS object Bigint which provides a way to represent whole numbers larger than (2^53)-1.
Top comments (3)
This is true, but there are typed arrays that allow you to work with binary data and the corresponding signed or unsigned integers.
That's the IEEE754 standard right here, and it's not only used in JS, but in a lot of other languages as well. It has its known limitations, but that's quite different from "quirks". Also, you seem to have forgotten typed arrays, which we have since ES6 (2015).
thanks for the update :)