JavaScript parseInt()Function
JavaScript Global Functions
Definition and Usage
The parseInt() function parses a string and returns an integer.
When the radix parameter is 0, or when this parameter is not set, parseInt() determines the base of the number based on the string.
When the radix parameter is omitted, JavaScript defaults the number's base as follows:
- If the string begins with "0x", parseInt() parses the rest of the string as a hexadecimal integer.
- If the string begins with 0, then ECMAScript v3 allows an implementation of parseInt() to parse the following characters as octal or hexadecimal numbers.
- If the string begins with a number from 1 to 9, parseInt() will parse it as a decimal integer.
Syntax
| Parameter | Description |
|---|---|
| string | Required. The string to be parsed. |
| radix | Optional. Represents the base of the number to be parsed. The value is between 2 and 36. |
Browser Support
![]()
![]()
![]()
![]()
![]()
All major browsers support the parseInt() function
Tips and Notes
Note:Only the first number in the string is returned.
Note:Leading and trailing spaces are allowed.
Note:If the first character of the string cannot be converted to a number, parseInt() returns NaN.
Note:When strings start with "0", older browsers default to an octal base. In ECMAScript 5, the default is a decimal base.
Examples
Examples
We will use parseInt() to parse different strings:
Output of the above examples:
Try it yourself »
Note:Older browsers use an older version of ECMAScript (ECMAScript versions earlier than ECMAScript 5 default to octal when a string starts with "0", while ECMAScript 5 uses decimal), so parsing ("010") will output 8.
JavaScript Global Functions Other extensions