ES2015 added four predicates and two parsers to Number. The parsers are the same functions as the globals (Number.parseInt === parseInt). The predicates, unlike the global isNaN() and isFinite(), never convert their argument, so a string is simply not NaN.
| Method | Returns true when the value is... | Global twin |
|---|---|---|
| Number.isNaN(v) | the number NaN | isNaN(v) converts first |
| Number.isFinite(v) | a number other than ±Infinity, NaN | isFinite(v) converts first |
| Number.isInteger(v) | a number with no fractional part | none |
| Number.isSafeInteger(v) | an integer within ±(2^53 - 1) | none |
| Number.parseFloat(s), Number.parseInt(s, radix) | (parse a leading number) | identical |
console.log(isNaN("abc"), Number.isNaN("abc"), isFinite("12"), Number.isFinite("12"));
console.log(Number.isInteger(5.0), Number.isInteger(5.5), Number.isSafeInteger(2 ** 53));
console.log(parseInt("ff", 16), parseInt("10a", 2), parseInt("z", 36));
console.log(parseInt(0.0000005), parseInt(1 / 0, 19));
console.log(["1", "2", "3"].map(parseInt), ["1", "2", "3"].map(Number));true false true false true false false 255 2 35 5 18 [ 1, NaN, NaN ] [ 1, 2, 3 ]
Both traps come from string conversion. 0.0000005 stringifies as "5e-7", so parseInt reads 5; 1 / 0 becomes "Infinity", and in base 19 the letter I is digit 18 while n is invalid. map(parseInt) passes the index as the radix (parseInt("2", 1) is NaN); use map(Number). Always pass a radix to parseInt, and use Math.trunc() to drop the fraction of a number.