Why is this an issue?

Numbers in JavaScript are stored as 64-bit floating-point. However, numbers that can’t be represented in such a format get rounded before assigning them to a variable.

What is the potential impact?

When you are saving a number that is too big or contains a fraction that is not expressible in the 64-bit floating-point format, it gets rounded which will lead to unexpected behaviours when it will be used.

How to fix it

When you need to store a large number, use BigInt(). Pay attention that operations between BigInt and Number types raise an error, so you will have to adapt your code accordingly.

For numbers with digits after the decimal, it is recommended to use a library to ensure that calculation errors are not introduced by rounding.

Code examples

Noncompliant code example

const foo = 2312123211345545367
const bar = 0.123456789123456789

Compliant solution

const foo = BigInt(2312123211345545367)
// use a library like decimal.js for storing numbers containing decimal digits

How does this work?

JavaScript numbers are stored in a double-precision 64-bit binary format IEEE 754. There are limits to what can be stored in this format both in huge numbers as in numbers with decimal digits. When attempting to store a number that can not be represented in this format into a variable, the number gets rounded before storage.

Resources

Documentation