The parseInt() function parses a string argument and returns an integer of the specified radix.
Example
parseInt() Syntax
The syntax of the parseInt() function is:
parseInt(string, radix)
parseInt() Parameters
The parseInt() function takes in:
- string - The value to parse. If it is not a string, it is converted to one using
ToStringabstract operation. - radix (optional) - An integer between 2 and 36 representing the base in the numeral system.
parseInt() Return Value
- Returns an integer parsed from the given string.
- Returns
NaNwhen:- radix is less than 2 or greater than 36.
- The first non-whitespace character can't be converted to a number.
Example: Using parseInt()
Output
875 15 15 -15 85 15 57 NaN NaN 464546416543075600
Note: If the radix parameter is undefined, 0, or unspecified, JavaScript considers the following:
- If the string begins with "0x", the
radixis 16 (hexadecimal). - If the string begins with "0", the
radixis 8 (octal) or 10 (decimal). The exactradixchosen is implementation-dependant - If the string begins with any other value, the
radixis 10 (decimal).
Recommended Reading: Javascript parseFloat()