JavaScript parseInt()

Example

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 »

Description

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.

Notes

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).


Syntax

parseInt(string, radix)

Parameters

Parameter Description
value Required.
The value to be parsed.
radix Optional. Default is 10.
A number (2 to 36) specifying the number system.

Return Value

Type Description
A number.NaN if no integer is found.


Browser Support

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

More Examples

Parse different values:

parseInt("10", 10);
parseInt("010");
parseInt("10", 8);
parseInt("0x10");
parseInt("10", 16);
Try it Yourself »

Copyright 1999-2023 by Refsnes Data. All Rights Reserved.