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");
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.
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST
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");
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.
ADVERTISEMENT
Browser Support
parseInt() is an ECMAScript1 (JavaScript 1997) feature.
It is 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);
JavaScript Global Reference
The JavaScript global properties and methods can be used with all JavaScript objects.
JavaScript Global Methods and Properties
Name Description
decodeURI() Decodes a URI
decodeURI
Component() Decodes a URI component
encodeURI() Encodes a URI
encodeURI
Component() Encodes a URI component
escape() Deprecated. Use instead:
encodeURI()
encodeURIComponent()
eval() Evaluates a string and executes it as if it was script code
Infinity A numeric value that represents positive/negative infinity
isFinite() Determines whether a value is a finite, legal number
isNaN() Determines whether a value is an illegal number
NaN "Not-a-Number" value
Number() Converts an object's value to a number
parseFloat() Parses a string and returns a floating point number
parseInt() Parses a string and returns an integer
String() Converts an object's value to a string
undefined Indicates that a variable has not been assigned a value
Top comments (0)