ParseInt & ParseFloat
parseInt() and parseFloat() read a number from the start of a string. They skip leading spaces, read as many characters as make sense as a number, and stop at the first one that does not. parseInt returns a whole number and parseFloat returns a number that may have a decimal part. If the string does not start with a number, both return NaN.
Reading from the left
Section titled “Reading from the left”console.log(parseInt('42'));console.log(parseInt('42 pages'));console.log(parseInt(' 42 '));console.log(parseInt('12.99'));console.log(parseFloat('12.99'));console.log(parseFloat('3.5 kg of flour'));console.log(parseFloat('1.2.3'));console.log(parseInt('Room 42'));This prints:
4242421212.993.51.2NaNparseInt('12.99') stops at the full stop, because a full stop is not part of a whole number, so the result is cut off at 12 rather than rounded. parseFloat('1.2.3') accepts the first full stop and stops at the second. 'Room 42' gives NaN, because the parsing stops at the very first character.
Both functions are also available as Number.parseInt and Number.parseFloat, which are the same functions under another name.
The radix argument
Section titled “The radix argument”parseInt takes a second argument, the radix, which is the number base the text is written in. It can be anything from 2 to 36:
console.log(parseInt('ff', 16));console.log(parseInt('101', 2));console.log(parseInt('0x1f'));console.log(parseInt('08'));console.log(parseInt('12', 10));This prints:
255531812Without a radix, parseInt reads the text as decimal, except that a 0x prefix makes it read hexadecimal. Very old engines read a leading 0 as octal, so '08' could fail, but ES5 removed that rule and every current engine reads '08' as 8. Passing 10 is still a good habit, since it states the intent and some linters ask for it. parseFloat has no radix and only reads decimal. Number Bases covers bases in more detail.
parseFloat compared with Number()
Section titled “parseFloat compared with Number()”Number() also converts text to a number, but it insists that the whole string is a number, apart from spaces at either end. parseFloat takes whatever number it finds at the start:
const inputs = ['25', '25kg', '', ' ', '2.5e3', '1_000'];
for (const text of inputs) { console.log(JSON.stringify(text), Number(text), parseFloat(text));}This prints:
"25" 25 25"25kg" NaN 25"" 0 NaN" " 0 NaN"2.5e3" 2500 2500"1_000" NaN 1Use Number() when the text should be a number and anything else is an error, such as a value from a form field, and use parseFloat when the number is followed by a unit or other text you want to ignore. Note that Number('') is 0, so an empty form field needs its own check. The underscore in 1_000 is allowed in number literals in code, as described in Numeric Separators, but not in strings that are parsed.
Checking the result
Section titled “Checking the result”A failed parse gives NaN, and Number.isNaN is the reliable test for it:
const quantity = parseInt('three', 10);
if (Number.isNaN(quantity)) { console.log('Not a number');}This prints:
Not a numberquantity === NaN cannot be used, because NaN is not equal to anything, itself included. NaN and isNaN explains why.
Two pitfalls
Section titled “Two pitfalls”parseInt converts its argument to a string before reading it, which goes wrong with numbers that print in exponential form. It also takes the radix as its second argument, which goes wrong when it is passed straight to map, because map passes the index as the second argument:
console.log(parseInt(0.0000005));console.log(String(0.0000005));console.log(Math.trunc(0.0000005));console.log(['1', '2', '3'].map(parseInt));console.log(['1', '2', '3'].map(Number));This prints:
55e-70[ 1, NaN, NaN ][ 1, 2, 3 ]To drop the fraction from a number that is already a number, use Math.trunc rather than parseInt. To convert an array of strings, use map(Number) or write the callback out in full as map((s) => parseInt(s, 10)).
Steve Griffith covers this topic in his video How to Use parseInt and parseFloat.