Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

parseInt

Converts a string to an integer.

Available since: ES1

Syntax

parseInt(string, radix)

Parameters

ParameterTypeDescription
stringstringThe string to parse.
radixnumberThe radix, from 2 to 36. Pass 10 for decimal numbers.

Return value

numberAn integer, or `NaN` if the string does not start with a digit.

Examples

Digits at the start are parsed and the rest is dropped:

console.log(parseInt('42px', 10));
console.log(parseInt('3.99', 10));

Output:

42
3

If the string does not start with a digit, the result is NaN:

console.log(parseInt('abc', 10));

Output:

NaN

Notes

The function drops the fraction. Use parseFloat() or Number() for fractional numbers.

See also

Related recipes

In the tutorial

  • Type conversion — How to turn a string into a number and back in JavaScript — Number, String, Boolean, parseInt and implicit conversion.

Thanks for reading!

Donate to the site — we work hard for you.