JavaScript — parseInt()

The parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems).

Syntax

parseInt(string, radix);

Parameters

stringThe value to parse. If this argument is not a string, then it is converted to one using the ToString abstract operation. Leading whitespace in this argument is ignored.

radixAn integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the string. Be careful ? this does not default to 10.

Return value: It returns a number and if the first character can?t be converted to a number then the function returns NaN. It actually returns a number parsed up to that point where it encounters a character which is not a number in the specified radix(base).Example:

Input: var n = parseInt(“2018@geeksforgeeks”);Output: n = 2018now n contains 2018 as ‘@’ is not a Number and parsing stops at that point,further characters are ignored.Input: var a = parseInt(“1000”);Output: a = 1000(Number)

Description

The parseInt function converts its first argument to a string, parses that string, then returns an integer or NaN.

If not NaN, the return value will be the integer that is the first argument taken as a number in the specified radix. (For example, a radix of 10 converts from a decimal number, 8 converts from octal, 16 from hexadecimal, and so on.) For radices above 10, letters of the English alphabet indicate numerals greater than 9. For example, for hexadecimal numbers (base 16), A through F are used.

If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. parseInttruncates numbers to integer values. Leading and trailing spaces are allowed.

Because some numbers use the e character in their string representation (e.g. 6.022e23 for 6.022 1023), using parseInt to truncate numbers will produce unexpected results when used on very large or very small numbers. parseInt should not be used as a substitute for Math.floor().

If the radix is undefined, 0, or unspecified, JavaScript assumes the following:

  1. If the input string begins with “0x” or “0X” (a zero followed by lowercase or uppercase X), radix is assumed to be 16 and the rest of the string is parsed as a hexidecimal number.
  2. If the input string begins with “0” (a zero), radix is assumed to be 8 (octal) or 10 (decimal). Exactly which radix is chosen is implementation-dependent. ECMAScript 5 clarifies that 10 (decimal) should be used, but not all browsers support this yet. For this reason always specify a radix when using parseInt.
  3. If the input string begins with any other value, the radix is 10 (decimal).

If the first character cannot be converted to a number, parseInt returns NaN unless the radix is bigger than 10.

For arithmetic purposes, the NaN value is not a number in any radix. You can call the isNaNfunction to determine if the result of parseInt is NaN. If NaN is passed on to arithmetic operations, the operation result will also be NaN.

To convert a number to its string literal in a particular radix, use thatNumber.toString(radix).

parseInt converts a BigInt to a Number and loses precision in the process because trailing non-numeric values, including “n”, are discarded.

parseInt(“100”,10) = 100parseInt(“8”,8) = NaNparseInt(“15”,8) = 13parseInt(“16″,16) = 22parseInt(” 100 “) = 100parseInt(“0x16”) = 22

Examples

Using parseInt

The following examples all return 15:

parseInt(‘0xF’, 16);parseInt(‘F’, 16);parseInt(’17’, 8);parseInt(021, 8);parseInt(‘015’, 10); // but parseInt(015, 10); will return 13parseInt(15.99, 10);parseInt(‘15,123’, 10);parseInt(‘FXX123’, 16);parseInt(‘1111′, 2);parseInt(’15 * 3′, 10);parseInt(’15e2′, 10);parseInt(’15px’, 10);parseInt(’12’, 13);

The following examples all return NaN:

parseInt(‘Hello’, 8); // Not a number at allparseInt(‘546’, 2); // Digits other than 0 or 1 are invalid for binary radix

The following examples all return -15:

parseInt(‘-F’, 16);parseInt(‘-0F’, 16);parseInt(‘-0XF’, 16);parseInt(-15.1, 10);parseInt(‘-17’, 8);parseInt(‘-15’, 10);parseInt(‘-1111’, 2);parseInt(‘-15e1’, 10);parseInt(‘-12’, 13);

The following examples all return 4:

parseInt(4.7, 10);parseInt(4.7 * 1e22, 10); // Very large number becomes 4parseInt(0.00000000000434, 10); // Very small number becomes 4

The following example returns 224:

parseInt(‘0e0’, 16);

BigInt values lose precision:

parseInt(900719925474099267n)// 900719925474099300

Source : MDN, stackoverflow, geeksforgeeks

20