Skip to content
the poor coder

parseInt() javascript

Poor Man
Published
Reading time
2 min read
On this page

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:

Text

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:

Text

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:

Text

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.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC