parseInt
Converts a string to an integer.
Available since: ES1
Syntax
parseInt(string, radix)Parameters
| Parameter | Type | Description |
|---|---|---|
| string | string | The string to parse. |
| radix | number | The 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
3If the string does not start with a digit, the result is NaN:
console.log(parseInt('abc', 10));Output:
NaNNotes
The function drops the fraction. Use parseFloat() or Number() for fractional numbers.
See also
- Number.prototype.toFixed — Formats a number with a given number of decimal places.
- Math.floor — Rounds a number down to an integer.
- parseFloat — Reads a floating-point number from the start of a string.
- Number() — Converts a value to a number.
- Number.prototype.toString — Converts a number to a string, including in another numeral system.
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.