# How does parseInt work in JavaScript

## parseInt ()

The method reads in a string argument and returns an integer in the specified number system.

### parameter

Convertible Value. If there is no string, it is converted to a string (by the abstract operation). Leading spaces in the string are ignored.
An integer between 2 and 36 that is the basis of a mathematical number system in which the string is written. 10 stands for the common decimal system.

### Return value

An integer from the string passed. If the first character cannot be converted to a number, it is returned.

### description

The function converts the first argument to a string, parses it and returns an integer or. If not returned, the value of the first argument is a valid integer in the appropriate number system. For example, a decimal number is assumed in the 10-digit system, an octal number in the 8-digit system, a hexadecimal number in the 16-digit system, and so on. For the base of a number system greater than, letters of the alphabet are representatives for numbers greater than. For example, the letters to are used for hexadecimal numbers (base 16).

If a character is found that is not a digit in the specified number system, this and all following characters are ignored. cuts off decimal places. Leading and trailing spaces are allowed.

Because some numbers have a character in their string representation (for example), the use of truncates the numeric value and produces unexpected results when using large or small numbers. should not be used as a substitute for.

If or is 0 (or missing), JavaScript assumes the following:

• If starts with or, the hexadecimal system is used.
• If starts with, the octal or decimal system is used (depending on the implementation). ECMAScript 5 specifies the decimal system, but not all browsers support it. This is why the parameter should always be specified
• If starts with another character, the decimal system is used.

If the first character cannot be converted to a number, returns.

For arithmetic purposes there is no number in any number system. The function can be called to check whether the result is from. When used in an arithmetic operation, the result is always.

To convert numbers to string representations in different number systems use the following:
.

### Use of

All of the following examples return:

All of the following examples return:

All of the following examples return:

The following examples return:

The following example returns:

Although ECMAScript 3 does not recommend it and ECMAScript 5 prohibits it, many implementations interpret a string beginning with an octal number. The following example has a result in the octal or decimal system. The number system should always be given to avoid such confusion.

### ECMAScript 5 removes octal interpretation

The ECMAScript 5 specification of the function no longer allows a string beginning with an octal number to be interpreted. ECMAScript 5 status:

The function produces an integer by interpreting the content of the string argument, depending on the number system specified in radix. Leading whitespaces are ignored. If radix or is assumed, unless the number begins with the characters or, then radix is ​​assumed.

This differs from the ECMAScript 3 specification, which does not recommend octal interpretations but allows them.

Because many implementations have not changed this behavior since 2013 and older browsers must also be supported, the radix argument should always be used.

### A strict parse function

It is sometimes useful to use a strict function for converting integers. Regular expressions can help:

### Browser compatibility

BCD tables only load in the browser