The built-in Number object holds a set of fixed values, called properties, that describe the limits of what JavaScript numbers can do. You read them straight off Number itself, like Number.MAX_VALUE. Knowing these limits explains a lot of "impossible" bugs.
| Number.MAX_SAFE_INTEGER | Largest exact whole number |
| Number.MIN_SAFE_INTEGER | Most negative exact whole number |
| Number.MAX_VALUE | Largest number of any kind |
| Number.MIN_VALUE | Smallest number above zero |
| Number.POSITIVE_INFINITY | Infinity |
| Number.NEGATIVE_INFINITY | -Infinity |
| Number.EPSILON | Tolerance for comparing decimals |
| Number.NaN | Not a Number |
JavaScript can store whole numbers exactly only up to a certain size. That size is 253 - 1, available as Number.MAX_SAFE_INTEGER (both safe-integer properties were added in ES6):
console.log(Number.MAX_SAFE_INTEGER);
console.log(Number.MIN_SAFE_INTEGER);
That is about 9 quadrillion. Past it, whole numbers start losing accuracy:
let max = Number.MAX_SAFE_INTEGER;
console.log(max + 1);
console.log(max + 2); // wrong! should end in 3
console.log(max + 1 === max + 2); // two different numbers, "equal"
Check whether a value is still in the safe zone with Number.isSafeInteger():
console.log(Number.isSafeInteger(9007199254740991));
console.log(Number.isSafeInteger(9007199254740992));
console.log(Number.MAX_VALUE); // largest possible number
console.log(Number.MIN_VALUE); // smallest number above zero
MAX_VALUE is far bigger than MAX_SAFE_INTEGER: JavaScript can hold numbers that large, just not exactly. e+308 means "times 10 to the power of 308": a number 309 digits long.
MIN_VALUE is the tiniest positive number: the closest you can get to zero without being zero. (Some tutorials, including W3Schools, describe it as "the lowest possible number", which is wrong.) The most negative number is -Number.MAX_VALUE.
console.log(Number.POSITIVE_INFINITY);
console.log(Number.NEGATIVE_INFINITY);
console.log(1 / 0); // what dividing by zero gives
console.log(Number.MAX_VALUE * 2); // overflow
Use Number.isFinite() to reject both infinities and NaN in one check:
console.log(Number.isFinite(100));
console.log(Number.isFinite(1 / 0));
console.log(Number.isFinite(NaN));
Number.isFinite(x) is a great one-line check that a calculation produced a real, usable number.
Number.NaN is the same value as the global NaN: a reserved word for "not a legal number". You get it from arithmetic with a non-numeric string:
console.log(Number.NaN);
console.log(100 / "Apple");
Number.EPSILON (ES6) is the difference between 1 and the smallest number greater than 1 that JavaScript can store: about 0.0000000000000002. It helps compare decimals that should be equal but are off by a rounding hair:
let a = 0.1 + 0.2;
let b = 0.3;
console.log(a === b);
console.log(Math.abs(a - b) < Number.EPSILON); // "close enough"
Math.abs() gives the distance between the two numbers, ignoring which is bigger. If that distance is smaller than EPSILON, treat them as equal.
These properties only exist on Number itself. Reading one off a variable or a value gives undefined, with no error:
let x = 6;
console.log(x.MAX_VALUE);
console.log(Number.MAX_VALUE);
Which is the most negative number JavaScript can store?
Number.MIN_VALUENumber.MIN_SAFE_INTEGER-Number.MAX_VALUENumber.EPSILONMIN_VALUE is the tiniest positive number. MIN_SAFE_INTEGER is the most negative number that is stored exactly, but much more negative numbers exist. (-Infinity is lower still, but it is not a real number.)What does this log?
let id = 9007199254740993;
console.log(id);
9007199254740992. The number is past MAX_SAFE_INTEGER, so it cannot be stored exactly and is rounded to the nearest number that can. That is how a big database ID can silently point to the wrong record.What does let score = 50; console.log(score.MAX_VALUE); log?
1.7976931348623157e+30850undefinedMAX_VALUE only exists on Number. Reading a property that does not exist gives undefined, not an error. Write Number.MAX_VALUE.max + 1, max + 2, and max + 3. Which results are wrong?Number.isFinite to check the results of 10 / 2, 10 / 0, and "a" / 20.1 + 0.2 to 0.3 using the EPSILON techniqueNumber.MIN_VALUE / 2. What happens when you go below the smallest positive number?