# How to Convert String to Number in JavaScript?

How to Convert String to Number in JavaScript? Here, we will discuss how to convert a string to a number in JavaScript. With examples, we’ll examine the various methods and their syntax. Programming’s essential, the foundational idea is data management. And using JavaScript to convert a string to a number is a common and straightforward procedure.

In JavaScript, a number can be represented as an integer (for instance, “7”) or even as a string. However, because they are two entirely distinct categories of items, using a rigid comparison method to compare and contrast the two would be ineffective.

``````var num1 = 4;
var num2 = '4';
if (num1 === num2) {
console.log(true);
}
else {
console.log(false);
}``````

Output:-

false

## Convert String to Number in JavaScript using the parseInt() Function

JavaScript offers us a number of simple ways to convert a string to a basic number. Let’s examine JavaScript’s parseInt() function for converting strings to numbers.

A string is the first input to the parseInt() function. Additionally, a base is required in order to transform the string. An integer will always be the value that is returned.

Syntax:-

The number system to be used is specified by a radix parameter: 16 is hexadecimal, 2 is binary, 8 is octal, and 10 is decimal. JavaScript presumes radix is 10 in the absence of radix. JavaScript uses radix 16 if the value starts with “0x”.

``````var num = '4';
var int = parseInt(num, 10);
console.log(int)``````

Output:-

4

## Convert String to Number in JavaScript using the parseFloat() Function

We can observe how JavaScript changes a text into a point number in this technique. A number with decimal points is known as a floating point number. JavaScript will also return the value if we pass strings with arbitrary content in them.

``````var ranNum = '2030IamTheBest';
var pointNum = parseFloat(ranNum);

console.log(ranNum)
console.log(pointNum)``````

Output:-

2022

## Convert String to Number in JavaScript by Multiplying String with 1

This approach is maybe the quickest way to get our transformed values. After multiplying a string with a number it is converted into a number type. If the given string doesn’t contain a number then after multiplication it produces NaN.

``````let str = '5423';
let floatStr = '22.524';
let nanStr = 'Know Program';

str = str * 1;
floatStr = floatStr * 1;
nanStr = nanStr * 1;

console.log(str)
console.log(typeof (str))
console.log(floatStr)
console.log(typeof (floatStr))
console.log(nanStr)
console.log(typeof (nanStr))``````

Output:-

5423
number
22.524
number
NaN
number

In order to receive the results in integer or point number format, we must multiply our strings by 1 in this approach. To achieve the same outcome, we can alternatively add using the ‘+’ operator as an option.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!