JavaScript Number parseInt() method
- Föregående sida parseFloat()
- Nästa sida prototype
- Åter till föregående nivå JavaScript Number Referenshandbok
Definition and usage
Number.parseInt()
The method parses the value as a string and returns the first integer.
radix The parameter specifies the numerical system to be used:
2 = Binary, 8 = Octal, 10 = Decimal, 16 = Hexadecimal.
If omitted radix, JavaScript assumes a base of 10. If the value starts with "0x", JavaScript assumes a base of 16.
Note:
If the first character cannot be converted to a number, it returns NaN
.
Leading and trailing spaces will be ignored.
It only returns the first integer found.
Instance
Example 1
Number.parseInt("10"); Number.parseInt("10.00"); Number.parseInt("10.33"); Number.parseInt("34 45 66"); Number.parseInt(" 60 "); Number.parseInt("40 years"); Number.parseInt("He was 40");
Example 2
Number.parseInt("10", 10); Number.parseInt("010"); Number.parseInt("10", 8); Number.parseInt("0x10"); Number.parseInt("10", 16);
Syntax
Number.parseInt(string, radix)
Parameter
Parameter | Description |
---|---|
value | Required. The value to be parsed. |
radix |
Optional. The default is 10. Specifies the numerical system of the value (2 to 36). |
Return value
Type | Description |
---|---|
Boolean value | If an integer is not found, it returns NaN. |
Browser support
Number.parseInt()
It is ECMAScript6 (ES6) features.
All modern browsers support ES6 (JavaScript 2015):
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
Stödjer | Stödjer | Stödjer | Stödjer | Stödjer |
Internet Explorer 11 (eller tidigare versioner) stöder inte Number.parseInt()
.
- Föregående sida parseFloat()
- Nästa sida prototype
- Åter till föregående nivå JavaScript Number Referenshandbok