What is the use of the parseInt() method in JavaScript?

The parseInt() method in JavaScript is used to convert a given value from a string to an integer. It takes two arguments: the value to be converted, and an optional radix (base) that specifies the numbering system to be used for parsing the string.

The parseInt() method can be used to extract numeric values from user inputs, perform calculations, manipulate numbers, and convert string representations of numbers to their numeric equivalents.

Syntax for the parseInt() method:

parseInt(string, radix);

Where:

Some usage of the parseInt() method:

1. Converting a string to an integer:

const str = "123";
const num = parseInt(str);
console.log(num); // Output: 123
console.log(typeof num); // Output: number

In this example, the parseInt() method is used to convert the string “123” to the integer 123. The resulting value is stored in the variable num, and the console.log() statements display the value and data type of num, which is a number.

2. Parsing a string with a radix:

const str1 = "1010";
const str2 = "A1";
const num1 = parseInt(str1, 2);
const num2 = parseInt(str2, 16);
console.log(num1); // Output: 10
console.log(num2); // Output: 161

In this example, the parseInt() method is used to parse two different strings with specified radix values. The string "1010" is parsed with a radix of 2, which represents binary, resulting in the integer 10. The string "A1" is parsed with a radix of 16, which represents hexadecimal, resulting in the integer 161.

3. Handling non-numeric characters:

const str = "123abc";
const num = parseInt(str);
console.log(num); // Output: 123

In this example, the string "123abc" contains non-numeric characters after the numeric value. However, the parseInt() method only converts the leading numeric characters and ignores the rest of the string after encountering non-numeric characters. Therefore, the resulting value of num is 123.

4. Converting user input to an integer:

const userInput = prompt("Enter a number:");
const num = parseInt(userInput);
console.log(num); // Output: Depends on user input

In this example, the parseInt() method is used to convert user input to an integer. The prompt() function is used to display a dialog box to the user, prompting them to enter a number. The value entered by the user is then passed to the parseInt() method to convert it to an integer.

5. Using the radix parameter for different numbering systems:

const binaryStr = "1010";
const octalStr = "755";
const hexadecimalStr = "1A";
const binaryNum = parseInt(binaryStr, 2);
const octalNum = parseInt(octalStr, 8);
const hexadecimalNum = parseInt(hexadecimalStr, 16);
console.log(binaryNum); // Output: 10
console.log(octalNum); // Output: 493
console.log(hexadecimalNum); // Output: 26

In this example, the parseInt() method is used to parse strings representing numbers in different numbering systems. The binaryStr variable represents a binary number “1010”, which is parsed with a radix of 2 to get the integer value 10. The octalStr variable represents an octal number “755”, which is parsed with a radix of 8 to get the integer value 493. The hexadecimalStr variable represents a hexadecimal number “1A”, which is parsed with a radix of 16 to get the integer value 26.

6. Handling NaN (Not a Number) value:

const str = "abc123";
const num = parseInt(str);
console.log(num); // Output: NaN
console.log(typeof num); // Output: number

In this example, the string "abc123" contains non-numeric characters at the beginning, resulting in a NaN value when parsed using the parseInt() method. NaN stands for "Not a Number" and represents an invalid numeric value. However, it is important to note that the typeof NaN is still "number", which can sometimes be misleading. Therefore, it is recommended to check for NaN using the isNaN() function if there is a possibility of getting non-numeric input.

7. Using the radix parameter to avoid unexpected results:

const num1 = parseInt("010"); // Output: 10 (in non-strict mode)
const num2 = parseInt("010", 10); // Output: 10
const num3 = parseInt("010", 8); // Output: 8

In this example, the string "010" is parsed using the parseInt() method without specifying a radix. In non-strict mode, JavaScript assumes an octal radix for strings with leading zeros, which may result in unexpected behavior. However, by specifying the radix explicitly as 10 (decimal) using the parseInt() method, the correct value of 10 is obtained. Similarly, when a radix of 8 (octal) is specified, the value of 8 is obtained.

Thank you for reading, and let’s have conversation with each other

Thank you for reading my article. Let’s have conversation on Twitter and LinkedIn by connecting.

Read more: