Manipulating string is easy in JavaScript as it provides users with a bunch of built-in methods for changing the values, removing characters and changing the data type and converting them into numbers.

Converting a given string into a number is useful and can be easily done through various JavaScript methods. This can be useful for developers so that they can manipulate values of string and perform calculations on these numbers easily. In this article we will discuss various methods provided by JavaScript to convert a string into a number, along with examples for better demonstration and understanding.

Ways to Convert a String into a Number

Sometimes we want to perform various operations on some numbers and these can not be done when the data type of these numeric values is String. That’s why JavaScript provides users with built-in methods to convert Strings into numbers and make the task of performing operations and different operators on these numbers easier.

Below are seven ways through which you can convert a provided string into a number, along with examples for your better understanding.

Using Number()

One of the most common methods of converting a string to a number is by using the Number() method which takes in a string parameter and returns the number value. In case the provided string can not be converted to a number then the method returns Not a Number (NaN) value, below is an example:

var example = “44.09”

var example2 = “45.00003”

var example3 = “46”


console.log(Number(example));

var a = Number(example2);


console.log(a)


console.log(Number(example3));

//check typeOf


console.log(typeof example2)


console.log(typeof a)

Output:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-convert-a-string-to-a-number-in-JavaScript-1.png" data-lazy- height="403" src="data:image/svg xml,” width=”422″>

Using parseInt()

Another method to use for converting a string to a number is by using parseInt() which takes in any numeric value as string and convert it into a number data type but if you pass a decimal number than it round off the number and returns an integer value as shown below through the following example:

var example = “445.7”

var example2 = “45.00003”

var example3 = “66”

var a = parseInt(example);

var b = parseInt(example2);

var c = parseInt(example3);

console.log(a)


console.log(b)


console.log(c)

//check typeOf


console.log(typeof example)


console.log(typeof a)

Output:

<img alt="" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-convert-a-string-to-a-number-in-JavaScript-2.png" height="184" src="data:image/svg xml,” width=”243″>

Using parseFloat()

Sometimes we want the decimal number to be returned exactly without any modification and for that we use the parseFloat() method. If you pass multiple numbers or a number with string then only the number is returned. But if you specify the string first and then a number, it’ll be unable for the method to convert it into a number and it’ll return NaN, as shown below through the example:

var example = “445.767”

var example2 = “45 50”

var example3 = “66 years”

var example4 = “year 1996”

var a = parseFloat(example);

var b = parseFloat(example2);

var c = parseFloat(example3);

var d = parseFloat(example4);

console.log(a)


console.log(b)


console.log(c)


console.log(d)

//check typeOf


console.log(typeof example3)


console.log(typeof c)

Output:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-convert-a-string-to-a-number-in-JavaScript-3.png" data-lazy- height="225" src="data:image/svg xml,” width=”321″>

Using Unary Operator ( )

Another way of converting a string into a number is by using the Unary Operator “ ” along with the variable that will convert the string into a number as shown by the example given below:

var example = “445.767”

var example2 = “45”

var a = example

var b = example2


console.log(a)


console.log(b)

//check type


console.log(typeof example)


console.log(typeof a)

Output:

<img alt="" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-convert-a-string-to-a-number-in-JavaScript-4.png" height="177" src="data:image/svg xml,” width=”252″>

Using Math.floor()

This method takes in a string and return the number data type, but for decimals it returns the integer part only as a number, as shown by example provided below:

var example = “445.7”

var example2 = “45”

var a = Math.floor(example)

var b = Math.floor(example2)


console.log(a)


console.log(b)

//check type


console.log(typeof example)


console.log(typeof a)

Output:

<img alt="" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-convert-a-string-to-a-number-in-JavaScript-5.png" height="173" src="data:image/svg xml,” width=”207″>

Using Double tilde (~~) Operator

Another way of simply converting a string into a number is by using double tilde “~~” along with the string which will convert any string into a number. In case of decimal values then it just simply takes the integer part and ignores the decimal part.

var example = “60.67”

var example2 = “33”

var a = ~~example

var b = ~~example2


console.log(a)


console.log(b)

//check type


console.log(typeof example)


console.log(typeof a)

Output:

<img alt="" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-convert-a-string-to-a-number-in-JavaScript-6.png" height="174" src="data:image/svg xml,” width=”244″>

Multiplication with a Number

Simplest way of converting a string into a number is by multiplying that string with 1, which will return a number. Both decimal and integer values will be returned with number as a data type shown through the below example:

var example = “60.67”

var example2 = “33”

var a = example*1

var b = example2*1


console.log(a)


console.log(b)

//check typeOf


console.log(typeof example)


console.log(typeof a)

Output:

<img alt="" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-convert-a-string-to-a-number-in-JavaScript-7.png" height="179" src="data:image/svg xml,” width=”236″>

Conclusion

There are various ways of converting a string into number, some built-in methods as well as some other operators to use for this purpose. In this article we discussed different built-in methods as well as some ways of converting a string into numbers. You can simply multiply the string by one which will convert it into a number or you can use built in methods as discussed in this article along with examples for your better understanding.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/7011409B3A384F43A8417D1DAC68D179-150×150.jpg6164f87de2e8e.jpg" height="112" src="data:image/svg xml,” width=”112″>

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.