English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript parseInt() function

 JavaScript Global Properties/Function

parseInt()The function's purpose is to parse the string argument and return the integer specified by the radix.

The radix parameter is used to specify the number system to be used, for example, radix16(Hexadecimal) indicates that the numbers in the string should be parsed from hexadecimal digits to decimal digits.

If the radix parameter is undefined (or does not exist), JavaScript assumes the following:

  • If the string starts with ' 0x', the radix is16(Hexadecimal)

  • If the string starts with ' 0', the radix is8(Octal)

  • If the string starts with any other value, the radix is10(Decimal)

If the first character cannot be converted to a number, parseInt() returns NaN.

If parseInt() encounters a character that is not a digit in the specified radix, it ignores the character and all subsequent characters, and returns the integer value parsed up to that point.

The parseInt() truncates the number to an integer value. It allows leading and trailing spaces...

To convert a number to a string representation in a specific radix, use intValue.toString(radix).

Syntax:

parseInt(string, radix)
parseInt("12");   // 12
parseInt("12.25");// 12
parseInt("15px"); // 15
parseInt("34 45 66"); // 34
parseInt("   20  "); // 20
parseInt("Parrot 12");// NaN
parseInt('314e-2');   // 3
parseInt('0.0314E+2');// 0
parseInt('13', 8);// 11
parseInt('10', 16);   // 16
parseInt('0xF', 16);  // 15
parseInt('1001', 2);  // 9
Test and see‹/›

Browser Compatibility

All browsers fully support the parseInt() function:

Function
parseInt()isisisisis

Parameter Value

ParameterDescription
string(Required) the string to be parsed
radix(Optional) between2to36integer between

Technical Details

Return Value:Returns an integer parsed from the given string. If the first character cannot be converted to a number, it returns NaN.
JavaScript Version:ECMAScript 1

 JavaScript Global Properties/Function