Parse different strings:
The result of n will be:
The parseInt() function parses a string and returns an integer.
The radix parameter is used to specify which numeral system to be used, for example, a radix of 16 (hexadecimal) indicates that the number in the string should be parsed from a hexadecimal number to a decimal number.
If the radix parameter is omitted, JavaScript assumes the following:
Note: Only the first number in the string is returned!
Note: Leading and trailing spaces are allowed.
Note: If the first character cannot be converted to a number, parseInt() returns NaN.
Note: Older browsers will result parseInt("010") as 8, because older versions of ECMAScript, (older than ECMAScript 5, uses the octal radix (8) as default when the string begins with "0". As of ECMAScript 5, the default is the decimal radix (10).
Function | |||||
---|---|---|---|---|---|
parseInt() | Yes | Yes | Yes | Yes | Yes |
Parameter | Description |
---|---|
string | Required. The string to be parsed |
radix | Optional. A number (from 2 to 36) that represents the numeral system to be used |
Return Value: | A Number. If the first character cannot be converted to a number, NaN is returned |
---|---|
JavaScript Version: | 1.0 |