JavaScript parseInt() Function
The parseInt() function reads a string and returns the whole number at its start, in the given base. It stops at the first character that isn't a digit.
Description
The parseInt() global function parses a string and returns an integer in the specified base (radix). It reads digits from the start of the string and stops at the first character that isn't valid, so parseInt('42px') returns 42. If no digits can be read, it returns NaN.
Always pass the radix, usually 10. Without it, strings starting with 0x are read as hexadecimal, which can lead to surprising results with user input. Decimals are cut off rather than rounded: parseInt('3.9') is 3.
To convert a whole string to a number and get NaN if any of it is invalid, use Number() instead. parseInt() is also available as Number.parseInt().
Syntax
parseInt(string)
parseInt(string, radix)Parameters
- stringThe value to parse. It is converted to a string first, and leading white space is ignored.
- radixOptional. A number from 2 to 36 giving the base of the number, such as 10 for decimal or 16 for hexadecimal.
Return Value
An integer parsed from the start of the string, or NaN if the first non-space character can't be turned into a number.
Example
Browser Support
The following information will show you the current browser support for the parseInt() function. Hover over a browser icon to see the version that first introduced support for this function.
Desktop





Tablets & Mobile





