Working with numbers in javascript is a bit tricky when compared to other programming languages. We will see why in a bit. So the structure of this blog is as follows.

1. Characteristics of Numbers in Javascript
2. Special Numbers
3. How to represent big numbers?

Characteristics of Numbers in Javascript

Numbers in Javascript are represented by a Fixed 64 bits length. These 64 bits are split and used in specified order to represent a number.

Sign — 1Bit

Exponent — 11Bits

Significand — 52Bits

This representation is called the double precision floating point format for representing numbers

The maximum safe limit of numbers that can be rendered in Javascript is

- 2⁵³-1 to +2⁵³-1
(or)
-9007199254740991 to +9007199254740991

Any number above that will give unpredicted results during comparisons and arithmetic operations such as

2⁵³ + 4 == 2⁵³ + 5

Secondly, due to the presence of the sign bit all numeric values in javascript are signed. That means zeros and infinities are also signed like+0, -0, +Infinity, -Infinity.

Thirdly, due to the presence of a exponent all the numeric values in javascript are floating points(decimals).

The double precision floating point format for numbers cannot represent some decimal numbers accurately. The reason is that all the numbers in this format are represented as two to the power of some whole number. Even the decimal parts of it. Because of this some decimal values such as 0.1 which is 1/10 whose denominator is not a power of two cannot be represented accurately in binary representation.
Because of this the following operation like

0.1 + 0.2 === 0.3
is false

Hence floating points arithmetic operations are not accurate in Javascript. Further reading on this an be found in the following links

Special Numbers

The Number web API is an object that contains all the basic funtionality required to perform some operation on numbers. Just type Number in your browser terminal to access it. It has some special number declaration in it. Some of them which can be useful in certain situations are as follows

Number.MAX_SAFE_INTEGER = 2⁵³ -1
Number.MIN_SAFE_INTEGER = -2⁵³ -1
Number.POSITIVE_INFINITY
Number.NEGATIVE_INFINITY
Number.NaN

How to represent big numbers

So now that we know we can’t represent numbers greater than 2⁵³-1 without breaking the arithmetic and comparable functionality over them, is there a way we can represent big numbers?
The BigInt spec was introduced to address this problem. This specification is still in the draft stage so do not use it in production environment. Some important things to note with respect to BigInt are

1. BigInt will not work with the default Math object available in javascript
2. You can typecast between BigInt and Number but it will lead to loss in precision.
3. BigInt supports +, -, *, /, **, Bitwise operators except(>>>)
4. The division of a BigInt with another BigInt will not give you fractional values. It will be truncated.

The details info on BigInt is available in the following link.

Thank you :-)

Senior Software Engineer — Frontend @Freshworks.

Senior Software Engineer — Frontend @Freshworks.