parseInt() javascript

What is parseInt() in JavaScript?

parseInt() is a built-in function in JavaScript that is used to convert a string to an integer. This function takes two arguments - the first argument is the string to be converted and the second argument is the radix or base number.

The radix can be any number between 2 and 36. If the radix is not specified, it defaults to 10. The radix is used to specify the base of the number system used in the string. For example, if the string represents a binary number, the radix would be 2.

How to use parseInt() in JavaScript?

To use parseInt() in JavaScript, you simply need to call the function and pass in the string and the radix as arguments. Here is an example:


let str = "10";
let num = parseInt(str, 10); // num will be 10
    

In this example, we are converting the string "10" to an integer using parseInt(). We are specifying the radix as 10, which means the string represents a decimal number.

How does parseInt() work in JavaScript?

When you call parseInt() in JavaScript, it first removes any whitespace characters from the beginning and end of the string. It then reads through the string from left to right until it encounters a non-numeric character.

The function then stops and returns the integer value of the characters it has read so far. If the first character in the string is not a number or a plus/minus sign, parseInt() returns NaN (Not a Number).

If the string contains a decimal point, parseInt() stops reading the string at that point and returns the integer value of the characters it has read so far. For example:


let str = "10.5";
let num = parseInt(str, 10); // num will be 10
    

In this example, we are trying to convert the string "10.5" to an integer using parseInt(). Since the string contains a decimal point, parseInt() stops reading the string at that point and returns the integer value of the characters it has read so far, which is 10.

Alternative ways to convert a string to an integer in JavaScript

  • Number() function: Another way to convert a string to an integer in JavaScript is to use the Number() function. This function works in a similar way to parseInt(), but it always assumes that the string represents a decimal number. If the string contains non-numeric characters, Number() returns NaN.
  • Unary plus (+) operator: You can also use the unary plus (+) operator to convert a string to an integer. This operator simply applies the built-in ToNumber() conversion function to the string. If the string contains non-numeric characters, the result will be NaN.

Here are examples of how to use these alternative methods:


let str = "10";
let num1 = Number(str); // num1 will be 10
let num2 = +str; // num2 will be 10
    

In these examples, we are converting the string "10" to an integer using the Number() function and the unary plus operator.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe