Parse different values:
parseInt("10");
parseInt("10.00");
parseInt("10.33");
parseInt("34 45 66");
parseInt(" 60 ");
parseInt("40 years");
parseInt("He was 40");
Try it Yourself »
The parseInt
method parses a value as a string and returns the first integer.
A radix parameter specifies the number system to use:
2 = binary, 8 = octal, 10 = decimal, 16 = hexadecimal.
If radix is omitted, JavaScript assumes radix 10. If the value begins with "0x", JavaScript assumes radix 16.
If the first character cannot be converted, NaN
is returned.
Leading and trailing spaces are ignored.
Only the first integer found is returned.
Older browsers will return 8 for parseInt("010"). Older versions of ECMAScript used octal (radix 8) for values beginning with "0". From ECMAScript 5 (2009) default is decimal (radix 10).
parseInt(
string, radix)
Parameter | Description |
value | Required. The value to be parsed. |
radix | Optional. Default is 10. A number (2 to 36) specifying the number system. |
Type | Description |
A number. | NaN if no integer is found. |
parseInt()
is an ECMAScript1 (ES1) feature.
ES1 (JavaScript 1997) is fully supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |
Parse different values:
parseInt("10", 10);
parseInt("010");
parseInt("10", 8);
parseInt("0x10");
parseInt("10", 16);
Try it Yourself »
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!