The parseInt function converts its first argument to a string, parses that string, then returns an integer or NaN . If not NaN, the return value will be the integer that is the first argument taken as a number in the specified radix. The radix argument specifies the numeral system to be used for the respective numeric string.
- str: A string data type argument that is to be parsed as an integer data type value.
- radix_value: It represents the base of the number in the string literal that is to be parsed. It is an optional argument in the parseInt() function.
Return Type: number or NaN
- If the string argument in the function starts with "" characters, the radix will be considered as , i.e., a hexadecimal value.
- If the string argument starts with a "" character, the radix will be considered as (octal value) in many ECMAScript3 implementations but this exception is deprecated in the latest ECMAScript.
- If the string argument starts with any other value/character, the radix value will be , i.e., considered as a decimal number.
- If the string argument contains an invalid number, that can't be parsed into an integer value, the function returns (Not a Number).
Explanation: In the above example, we are passing a binary numeric string value "100111" with base 2 (radix_value = 2) to the parseInt() function. 100111 will be parsed as a binary number and it will be converted to a decimal number 39 with base 10.
Let's understand the conversion of "100111" with radix 2 to an integer of base 10. Here, binary to decimal conversion is performed by the parseInt() function.
Octal Interpretations with No Radix
Please keep in mind that the information below does not apply to newer implementations as of 2021.
Many ECMAScript3 implementations had interpreted a numeric string beginning with a 0 in the string literal as an octal value, although the numeric string starting with 0 is considered to be a decimal value in the latest ECMAScript6. The following may have yielded an octal or decimal result:
Output: (with ECMAScript 3)
- Both the string literals are starting with , so these are considered octal numbers.
- string literal is evaluated till the first , when a char appears, it stops the parsing of the string as it is not present in the octal representation, so it will output only.
- With the string literal, is also not present in the octal representation, so it will stop at and will give the output .
Output: (with the latest ECMAScript after 2021)
- These numbers are considered decimal numbers in the latest ECMAScript.
- Here also, the string literal is evaluated till the first , when the char appears, it stops the parsing of the string as it is not present in the decimal representation, so it will output only.
- With the string literal, and both are present in the decimal representation, so it will give the output .
A Stricter Parse Function
It's occasionally advantageous to be able to parse numbers more precisely. It will return a parsed integer equivalent, or it will return NaN. Here, the Number() function is used to get an integer value out of the string literal and it can only accept one argument, i.e., a string literal. It does not take the radix value as a parameter.
We are using a regular expression in the below example to test the values.
The above regular expression will check, if the string starts with a or sign or not ( denotes not necessarily required), and the group (\d+|Infinity)$ denotes that the string should end with digits or word, then only the string will be accepted.
Explanation: Output is strictly based on the regular expression and it returns an integer value with base 10, if the regex test value is true, else returns NaN (Not a Number).
2. Add Two Strings with and without Using parseInt() Method.
Explanation: The above example clearly explains how two strings can be added using parseInt() and without using parseInt().
3. Pass Radix Argument within parseInt() Method
Passing a base (radix_value) of the number in the string literal.
Explanation: All the console outputs are decimal numbers with base 10, parseInt() takes the string literal with their respective radix_value and converts them to an integer with base 10 decimal numbers.
- It can take two parameters, the first is the string literal, and the second is the radix_value, i.e., the base of that number.
- radix_value is an optional parameter to the parseInt() function.
- parseInt() will return NaN (Not a Number) if the radix_value is less than 2 or greater than 36.