JavaScript Number parseInt() method

Definition and usage

Number.parseInt() The method parses the value as a string and returns the first integer.

radix The parameter specifies the numeric system to be used:

2 = Binary, 8 = Octal, 10 = Decimal, 16 = Hexadecimal.

If omitted radix, JavaScript assumes a radix of 10. If the value starts with "0x", JavaScript assumes a radix 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");

Try it yourself

Example 2

Number.parseInt("10", 10);
Number.parseInt("010");
Number.parseInt("10", 8);
Number.parseInt("0x10");
Number.parseInt("10", 16);

Try it yourself

Syntax

Number.parseInt(string, radix)

Parameter

Parameter Description
value Required. The value to be parsed.
radix

Optional. The default is 10.

Specifies the numeric system value (from 2 to 36).

Return value

Type Description
Boolean value If no integer is found, it returns NaN.

Browser support

Number.parseInt() It is an ECMAScript6 (ES6) feature.

All modern browsers support ES6 (JavaScript 2015):

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
Supported Supported Supported Supported Supported

Internet Explorer 11 (or earlier versions) does not support Number.parseInt().