parseInt() in JavaScript – How to Convert String to Decimal
Whenever you invoke parseInt(), the method does the following:
- It parses (analyzes) its string argument.
- It converts the string argument from its radix state to a decimal integer.
Use JavaScript’s parseInt() method to convert strings from their radix state to a decimal integer.
Syntax of the parseInt()
Method
parseInt()
accepts two arguments. Here is the syntax:
parseInt("string", radix);
Argument 1: string
A string
argument is the first argument accepted by the parseInt()
method. It is a required argument specifying the string value you want the computer to parse (analyze).
Argument 2: radix
A radix
is the second argument accepted by the parseInt()
method. It is an optional argument specifying the string argument’s base.
Example 1: Convert Binary String to Decimal Integer
parseInt("1011001", 2);
// The invocation above will return: 89
Example 2: Convert Hexadecimal String to Decimal Integer
parseInt("4fc", 16);
// The invocation above will return: 1276
Example 3: Convert Decimal String to Decimal Integer
parseInt("45", 10);
// The invocation above will return: 45
Note that the snippet above is equivalent to:
parseInt("45");
// The invocation above will return: 45
Important Stuff to Know about the Radix Argument
Below are three essential pieces of info you need to know about the radix
argument.
Info 1: Always provide a radix
argument
It is best practice to specify the radix
argument because it does not always default to base 10 (decimal).
For instance, suppose you omit the radix
and begin the string
argument with "0X"
or "0x"
(number zero followed by the upper-case or lower-case letter X). In that case, the computer will use 16
(hexadecimal) as the radix.
Example 1: Begin parseInt()
’s string argument with "Ox"
without providing a radix
parseInt("0x95");
// The invocation above will return: 149
Note that the snippet above is equivalent to:
parseInt("0x95", 16);
// The invocation above will return: 149
Example 2: Begin parseInt()
’s string argument with "Ox"
while providing a radix
parseInt("0x95", 35);
// The invocation above will return: 40745
Info 2: Radix must be an integer between 2
and 36
inclusive
The radix
argument must be an integer between 2
and 36
inclusive. Otherwise, parseInt()
will return NaN
(Not a Number).
Example 1: 1
is an invalid radix value
parseInt("45", 1);
// The invocation above will return: NaN
Example 2: 37
is an invalid radix value
parseInt("45", 37);
// The invocation above will return: NaN
Example 3: 36
is a valid radix value
parseInt("45", 36);
// The invocation above will return: 149
Example 4: 18
is a valid radix value
parseInt("45", 18);
// The invocation above will return: 77
Info 3: The first invalid character ends parseInt()
’s analysis
Suppose parseInt()
encounters a character that is not a valid number in the specified radix
numeral system. In such a case, the computer will do the following:
- It will ignore all characters from the invalid one to the end of the string.
- It will convert only the characters preceding the invalid one.
Example 1: The first character is not a binary numeral
parseInt("5111", 2);
// The invocation above will return: NaN
The snippet above returned NaN
because the string argument’s first character (5
) is an invalid binary numeral. Therefore, the computer ignored all the string’s characters.
Example 2: The second character is not a binary numeral
parseInt("1511", 2);
// The invocation above will return: 1
The snippet above returned 1
because the string argument’s second character (5
) is an invalid binary numeral. Therefore, the computer converted only the first character.
Example 3: The third character is not a binary numeral
parseInt("1151", 2);
// The invocation above will return: 3
The snippet above returned 3
because the string argument’s third character (5
) is an invalid binary numeral. Therefore, the computer converted only the first and second characters.
Example 4: The fourth character is not a binary numeral
parseInt("1115", 2);
// The invocation above will return: 7
The snippet above returned 7
because the string argument’s fourth character (5
) is an invalid binary numeral. Therefore, the computer converted only the first, second, and third characters.