Skip to content
Latest: Publish JavaScript Packages to NPM Like a Pro!

parseInt() in JavaScript – How to Convert String to Decimal

Whenever you invoke parseInt(), the method does the following:

  1. It parses (analyzes) its string argument.
  2. It converts the string argument from its radix state to a decimal integer.

JavaScript parseInt() global method's
tidbit

Use JavaScript’s parseInt() method to convert strings from their radix state to a decimal integer.

parseInt() accepts two arguments. Here is the syntax:

parseInt("string", radix);

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

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

Section titled “Example 1: Convert Binary String to Decimal Integer”
parseInt("1011001", 2);
// The invocation above will return: 89

Try Editing It

Example 2: Convert Hexadecimal String to Decimal Integer

Section titled “Example 2: Convert Hexadecimal String to Decimal Integer”
parseInt("4fc", 16);
// The invocation above will return: 1276

Try Editing It

Example 3: Convert Decimal String to Decimal Integer

Section titled “Example 3: Convert Decimal String to Decimal Integer”
parseInt("45", 10);
// The invocation above will return: 45

Try Editing It

Note that the snippet above is equivalent to:

parseInt("45");
// The invocation above will return: 45

Try Editing It

Important Stuff to Know about the Radix Argument

Section titled “Important Stuff to Know about the Radix Argument”

Below are three essential pieces of info you need to know about the 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

Section titled “Example 1: Begin parseInt()’s string argument with "Ox" without providing a radix”
parseInt("0x95");
// The invocation above will return: 149

Try Editing It

Note that the snippet above is equivalent to:

parseInt("0x95", 16);
// The invocation above will return: 149

Try Editing It

Example 2: Begin parseInt()’s string argument with "Ox" while providing a radix

Section titled “Example 2: Begin parseInt()’s string argument with "Ox" while providing a radix”
parseInt("0x95", 35);
// The invocation above will return: 40745

Try Editing It

Info 2: Radix must be an integer between 2 and 36 inclusive

Section titled “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).

parseInt("45", 1);
// The invocation above will return: NaN

Try it on CodePen

parseInt("45", 37);
// The invocation above will return: NaN

Try it on CodePen

parseInt("45", 36);
// The invocation above will return: 149

Try Editing It

parseInt("45", 18);
// The invocation above will return: 77

Try Editing It

Info 3: The first invalid character ends parseInt()’s analysis

Section titled “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:

  1. It will ignore all characters from the invalid one to the end of the string.
  2. It will convert only the characters preceding the invalid one.

Example 1: The first character is not a binary numeral

Section titled “Example 1: The first character is not a binary numeral”
parseInt("5111", 2);
// The invocation above will return: NaN

Try it on CodePen

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

Section titled “Example 2: The second character is not a binary numeral”
parseInt("1511", 2);
// The invocation above will return: 1

Try Editing It

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

Section titled “Example 3: The third character is not a binary numeral”
parseInt("1151", 2);
// The invocation above will return: 3

Try Editing It

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

Section titled “Example 4: The fourth character is not a binary numeral”
parseInt("1115", 2);
// The invocation above will return: 7

Try Editing It

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.