DEV Community

Cover image for Yes! There is no integer data type in JavaScript.
saiarlen
saiarlen

Posted on

3 2

Yes! There is no integer data type in JavaScript.

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, weโ€™ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, weโ€™ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (3)

Collapse
 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ โ€ข

This is true, but there are typed arrays that allow you to work with binary data and the corresponding signed or unsigned integers.

Collapse
 
lexlohr profile image
Alex Lohr โ€ข

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).

Collapse
 
saiarlen profile image
saiarlen โ€ข

thanks for the update :)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

๐Ÿ‘‹ Kindness is contagious

Please leave a โค๏ธ or a friendly comment on this post if you found it helpful!

Okay