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

## Syntax of the `parseInt()` Method

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

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

Try Editing It

Try Editing It

## Example 3: Convert Decimal String to Decimal Integer

Try Editing It

Note that the snippet above is equivalent to:

Try Editing It

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

Try Editing It

Note that the snippet above is equivalent to:

Try Editing It

## Express Your Love for Coding!

Explore CodeSweetly's Shop for an array of stylish products to enhance your coding experience.

Try Editing It

### 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

Try it on CodePen

#### Example 2: `37` is an invalid radix value

Try it on CodePen

Try Editing It

Try Editing It

### 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

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

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

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

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.