What is the smallest number that can be represented without a problem in JavaScript, for integers?
Number.MIN_SAFE_INTEGER
is a constant in JS that represents the smallest possible value that can be safely used, that is (-(2^53 - 1))
or -9007199254740991
For values smaller that this, BigInt
can be used
“Safe” refers to the ability of JavaScript to represent integers exactly and to correctly compare them.
Example: Value of Number.MIN_SAFE_INTEGER
value = Number.MIN_SAFE_INTEGER;
console.log(value); // -9007199254740991
value_minus_1 = value - 1;
value_minus_2 = value - 2;
// JS Number cannot exactly represent integers smaller than 'value'
// and correctly compare them
console.log(value_minus_1 == value_minus_2); // true
Output
-9007199254740991
true
Why does this happen!
The reasoning behind this number is that JavaScript uses double-precision floating-point format numbers as specified in IEEE 754 and can only safely represent numbers between -(2^53 - 1)
and 2^53
Related
Recommended Readings:
Reference: