A string is nothing but a sequence of characters put together. There are multiple operations that can be performed on the string. In this article, we will learn about various techniques to convert String to Number in JavaScript. Let’s get started!
Revisiting Strings in JavaScript
Strings in JavaScript represent a sequence of multiple characters. ‘C’ represents a single character and “Cat” represents a string.
In JavaScript, we can declare a string in two ways. First, we can declare it in JavaScript by making a string literal using double quotes. The syntax is given below:
//declaring a string literal let a="FavTutor" //printing the string console.log(a);
Output:
FavTutor
Second, we can also declare it in JavaScript by creating a String object. An example is given below:
//declaring a string using String Object let str=new String("FavTutor") //printing the string console.log(str);
Output:
[ String: ‘FavTutor’ ]
6 Methods to Convert String to Number in JavaScript
In this section, we will take a look at various techniques that help us convert a JavaScript String into a Number. Let’s begin!
1) Using Number() Function
The easy method to convert a string to a number in JavaScript is by using a simple “Number()” function.
The syntax of the Number() function is as follows:
let num = Number(value);
where,
num: a variable to store the number we get after passing any value as a parameter
value: can be a string, float, etc.
Here’s an example to understand it well:
//declaring a variable which is a string let str="15"; //printing the "str" variable and it's type console.log(str + " " + typeof(str)); //using the Number() function let num=Number(str); //printing the "num" variable and it's type console.log(num + " " + typeof(num));
Output:
15 string
15 number
In this example, we have first taken a str variable to store a string equal to “15”. We checked the type of the variable using the “typeof()” function. We used the Number() function for this conversion. We finally check the type of the “num” variable to verify whether the Number() function has converted it or not. We have obtained the desired result.
Now, what if we try to pass a character string in the Number() function, instead of a number string? Let us check through an example:
//declaring a variable which is a string let str="FavTutor"; //printing the "str" variable and it's type console.log(str + " " + typeof(str)); //using the Number() function let num=Number(str); //printing the "num" variable and it's type console.log(num + " " + typeof(num));
Output:
FavTutor string
NaN number
As the character string does not correspond to any number, we get the output as “NaN” which means “Not-a-Number”. NaN is basically a number that is not logically or legally a Number.
2) Using parseInt() and parseFloat() Functions
We can also convert it to a number by using the parseInt() and parseFloat() functions. parseInt() returns a number or NaN. parseFloat returns a float or NaN.
Syntax of parseInt() Function:
let num = parseInt(value, radix)
where,
value: can be string, float, etc.
radix(optional): can be 2(binary), 8(octal), 10(decimal), or 16(hexadecimal) according to the requirement. The default is 10.
Here’s an example of converting a string number to an actual number using the parseInt() function:
//declaring a variable which is a string let strNum="13"; //printing the "strNum" variable and it's type console.log(strNum + " " + typeof(strNum)); //using the parseInt() function with different radix let convertedNum=parseInt(strNum,10); let convertedNumBin=parseInt(strNum,2); let convertedNumOct=parseInt(strNum,8); //printing the "convertedNum" variables and their type console.log(convertedNum + " " + typeof(convertedNum)); console.log(convertedNumBin + " " + typeof(convertedNumBin)); console.log(convertedNumOct + " " + typeof(convertedNumOct));
Output:
13 string
13 number
1 number
11 number
We have tried out different radices with the parseInt() function and got the desired results.
The syntax of parseFloat() is as follows:
let num = parseFloat(value);
where
value: can be string, float, etc.
Let us perform the parseFloat() function:
//declaring a variable which is a string let strFloat="13.65"; //printing the "strFloat" variable and it's type console.log(strFloat + " " + typeof(strFloat)); //using the parseFloat() function let convertedNumFloat=parseFloat(strFloat); //printing the "convertedNumFloat" variable and it's type console.log(convertedNumFloat + " " + typeof(convertedNumFloat));
Output:
13.65 string
13.65 number
We have got the desired output as a float.
Note that if we pass a float number in the parseInt() function, it will return only the integer part of the float.
3) Using the Math.floor() and Math.ceil() Functions
The Math.floor() function is a mathematical method in JavaScript that rounds a number down to the nearest integer. The Math.ceil() function is a mathematical method that rounds a number up to the nearest integer.
Both of these methods can help to convert a string to a number.
Here’s an example of using the Math.floor() function:
//declaring a variable which is a string let strFloat="13.65"; //printing the "strFloat" variable and it's type console.log(strFloat + " " + typeof(strFloat)); //converting string to number using Math.floor() let convertedNumFloat=Math.floor(strFloat); //printing the "convertedNumFloat" variable and it's type console.log(convertedNumFloat + " " + typeof(convertedNumFloat));
Output:
13.65 string
13 number
Math.floor() rounds down to the nearest integer. So, 13.65 is rounded down to 13. We have obtained the desired result.
Here’s an example of using the Math.ceil() function:
//declaring a variable which is a string let strFloat="13.65"; //printing the "strFloat" variable and it's type console.log(strFloat + " " + typeof(strFloat)); //converting string to number using Math.ceil() let convertedNumFloat=Math.ceil(strFloat); //printing the "convertedNumFloat" variable and it's type console.log(convertedNumFloat + " " + typeof(convertedNumFloat));
Output:
13.65 string
14 number
Math.ceil() rounds up to the nearest integer. So, 13.65 is rounded up to 14. We have obtained the desired result.
4) Using the Unary Plus Operator
The unary plus operator (+) is a simple and concise way to convert a string to a number in JavaScript. Adding the “+” operator before a string in JavaScript automatically converts the string to a numeric value.
Let us understand with the help of an example:
//declaring a variable which is a string let strNum="13"; //printing the "strNum" variable and it's type console.log(strNum + " " + typeof(strNum)); //converting string to number using unary operator let convertedNum=+strNum; //printing the "convertedNum" variable and it's type console.log(convertedNum + " " + typeof(convertedNum));
Output
13 string
13 number
We have first taken a “strNum” variable to store a string equal to “13”. We checked the type of the variable using the “typeof()” function. We finally check the type of the “convertedNum” variable to verify whether the string has been converted to a number.
We have obtained the desired result. This method works for the float values in the same way.
//declaring a variable which is a string let strFloat="13.65"; //printing the "strFloat" variable and it's type console.log(strFloat + " " + typeof(strFloat)); //converting string to number using unary operator let convertedNumFloat=+strFloat; //printing the "convertedNumFloat" variable and it's type console.log(convertedNumFloat + " " + typeof(convertedNumFloat));
Output
13.65 string
13.65 number
The string “13.65” has been converted to a float number 13.65.
5) Subtracting the Number 0 from the String
The next approach to convert a string to a number is by subtracting 0 from the string. JavaScript automatically performs a type conversion, converting a string to a number.
Let’s see an example:
//declaring a variable which is a string let strNum="13"; //printing the "strNum" variable and it's type console.log(strNum + " " + typeof(strNum)); //converting string to number by subtracting 0 from it let convertedNum=strNum-0; //printing the "convertedNum" variable and it's type console.log(convertedNum + " " + typeof(convertedNum));
Output:
13 string
13 number
Even for float, this method works. Here’s how:
//declaring a variable which is a string let strFloat="13.65"; //printing the "strFloat" variable and it's type console.log(strFloat + " " + typeof(strFloat)); //converting string to number by subtracting 0 from it let convertedNumFloat=strFloat-0; //printing the "convertedNumFloat" variable and it's type console.log(convertedNumFloat + " " + typeof(convertedNumFloat));
Output:
13.65 string
13.65 number
We have obtained the desired output.
6) Multiplying and Dividing the String by 1
Another approach is by multiplying or dividing the string value by 1. JavaScript automatically performs a type conversion, converting the string into a number.
Multiplying by 1
Here’s an example of converting a string number to an actual number by multiplying it with 1.
//declaring a variable which is a string let strNum="13"; //printing the "strNum" variable and it's type console.log(strNum + " " + typeof(strNum)); //converting string to number by multiplying with 1 let convertedNum=strNum*1; //printing the "convertedNum" variable and it's type console.log(convertedNum + " " + typeof(convertedNum));
Output:
13 string
13 number
This method also works with a float string and returns a float output. Here’s an example.
//declaring a variable which is a string let strFloat="13.45"; //printing the "strFloat" variable and it's type console.log(strFloat + " " + typeof(strFloat)); //converting string to number by multiplying with 1 let convertedNumFloat=strFloat*1; //printing the "convertedNum" variable and it's type console.log(convertedNumFloat + " " + typeof(convertedNumFloat));
Output:
13.45 string
13.45 number
Dividing by 1
Here’s an example of this conversion by dividing it by 1:
//declaring a variable which is a string let strNum="13"; //printing the "strNum" variable and it's type console.log(strNum + " " + typeof(strNum)); //converting string to number by dividing by 1 let convertedNum=strNum/1; //printing the "convertedNum" variable and it's type console.log(convertedNum + " " + typeof(convertedNum));
Output:
13 string
13 number
This method also works with a float string and returns a float output. Here’s an example.
//declaring a variable which is a string let strFloat="13.45"; //printing the "strFloat" variable and it's type console.log(strFloat + " " + typeof(strFloat)); //converting string to number by dividing with 1 let convertedNumFloat=strFloat/1; //printing the "convertedNum" variable and it's type console.log(convertedNumFloat + " " + typeof(convertedNumFloat));
Output:
13.45 string
13.45 number
We have obtained the desired result.
You can also learn how to convert string to array in an easier way.
Conclusion
In this article, we discussed various JavaScript techniques to convert a string to a number. It is essential to know at least 3 of the methods when working in JavaScript as they come in very handy at times. You can check out this article to learn how to convert a set to an array in JavaScript.