JavaScript

# How to convert a string into an Integer in JavaScript

In JavaScript, there are multiple functions to convert String data into numeric data, for example, the Number() function, parseInt() function, parseFloat() function and so on. Now if we specifically talk about converting string data type into integer data type, it can be achieved using the JavaScript parseInt() function.

Mostly, we convert the string data type into integer conversion when we have to perform some mathematical tasks over the strings containing numeric values.

This post will explore the below-listed aspects to understand how to convert string-type data to integer-type data in JavaScript:

So, let’s begin!

## What is parseInt() function?

In JavaScript, parseInt() is a built-in function that converts/parses the string-type data to integer-type data. The parseInt() function takes a string value as a parameter and parses it into the integer value.

## Syntax of parseInt()

Let’s have a look at the below piece of code for a profound understanding of the parseInt() function:

Here, the radix is an optional parameter that shows which number system will be used, i.e., binary, hexadecimal, etc.

## How to use parseInt() function in JavaScript?

Let’s consider the below-given example to understand how parseInt() function work in JavaScript:

var value = "112";

console.log("Original Value" , value);

console.log("type of original value: " , typeof (value));

convertedValue = parseInt(value);

console.log("Converted Value" , convertedValue);

console.log("type of converted value: ", typeof (convertedValue));

In the example program, firstly, we printed the original value and its data type, afterward we utilized the parseInt() function to parse/convert the string type data to integer. Finally, we printed the converted value and type of converted value using typeof operator:

The output verified that initially, the variable’s type is a string; however, the parseInt() function converted the string into an integer.

### The below-given code snippet will provide you with more clarity about the parseInt() function:

var value1 = "112";

var value2 = 12;

console.log("First Value: " , value1);

console.log("Second Value: " , value2);

console.log("Add original values: " , value1 + value2);

convertedValue = parseInt(value1);

console.log("Add Converted Values: " , value2 + convertedValue);

The above example performed the following tasks:

• Initially, we printed the original values on the browser’s console,
• Next, we performed addition on the original values.
• Afterward, we converted the first value using the parseInt() function.
• Finally, we printed the addition of the second value and the converted value on the browser’s console:

From the above snippet, we observed that both values got concatenated when we performed addition on the original values. When we performed addition on the converted values, we got the desired result, i.e., the sum of both values.

## Conclusion

In JavaScript, parseInt() is a built-in function that converts/parses the string-type data to integer-type data. The parseInt() function takes a string value as a parameter and parses it into the integer value. This post presented a comprehensive guide on how to convert a string into an integer in JavaScript using some suitable examples.