# parseInt function

The `parseInt` function converts a string to an integer. This is necessary for values like `'12px'` - when first there is a number, and then units of measurement. If you apply the `parseInt` function to `'12px'`, then the result will be the number `12` (and it will really be a number, not a string). The conversion will only occur if the integer is at the beginning of the string, otherwise `NaN` will be displayed. As a second parameter, you can specify a number system of a number, and the function will return the number converted from the specified number system to decimal.

## Syntax

`parseInt(string, [number system]);`

## Example

Let's convert a string to an integer:

`console.log(parseInt('10px'));`

The code execution result:

`10`

## Example

When converting a fraction, a fractional part will be discarded:

`console.log(parseInt('10.3px'));`

The code execution result:

`10`

## Example

In this example, the function will not be able to read the number (because it is not at the beginning of the string) and will return `NaN`:

`let str = 'width: 100px;'; console.log(parseInt(str));`

The code execution result:

`NaN`

## See also

- the `parseFloat` function
  that extracts a fractional number from the beginning of a string
- the `Number` function
  that converts to a number

↑
en ru