DEV Community

Sam-Wisdoms  Amenyenu
Sam-Wisdoms Amenyenu

Posted on • Updated on

How to Convert a String to a Number in JavaScript

When it comes to converting string to number in JavaScript, there are many ways by which you can achieve that.
In this article, we are going to explore; five(5) ways by which you can convert a string to a number in JavaScript.

How to convert a string to a number using the plus (+) operator.

One of the ways by which you can convert a string into a number in JavaScript is by using the unary plus (+) operator or simply by using the plus symbol.

Let's see an example of how this looks like in code.

In this example, we are using the let keyword to declare a variable called stringToNumber with a value of "25" which is a string.

Let's see how to convert the string "25" to a number,

let stringToNumber = "25";
console.log(+stringToNumber);// 25
console.log(typeof stringToNumber) // string
Enter fullscreen mode Exit fullscreen mode

Assuming you have a variable called word set to a value of "I am a word" which is a string as below;

let word = "I am a word"
console.log(word) // I am a word
console.log(typeof word) // string
Enter fullscreen mode Exit fullscreen mode

Try to convert the word into a number, using the plus (+) operator as below;

let word = "I am a word"
console.log(+word)// NaN (Not a Number)
console.log(typeof word) // string
Enter fullscreen mode Exit fullscreen mode

Converting string to a number using the number() method or function.

Let's see in code how this works:

let stringToNumber = "25";
Enter fullscreen mode Exit fullscreen mode

From our earlier example, we know

console.log(typeof stringToNumber); // string
Enter fullscreen mode Exit fullscreen mode

Now, when we do

console.log(Number(stringToNumber)); // 25
Enter fullscreen mode Exit fullscreen mode

Keep in mind the Number() is case sensitive so Number() is not the same as number().

Let's see what will be printed to the console using the **Number() method if it is a word using our let word = "I am a word" variable declaration as above.

let word = "I am a word";
console.log(Number(word)); // NaN just as in the case of the plus(+) operator above.
Enter fullscreen mode Exit fullscreen mode

Try it yourself.

Converting a string to a number using the parseInt() function method.

Another way of converting a string to a number is by using the parseInt() function.
Check it out in the code below using our stringToNumber example:

let stringToNumber = "25";
console.log(parseInt(stringToNumber); // 25.
Enter fullscreen mode Exit fullscreen mode

parseInt() method using our word example:

let word = "I am a word"
console.log(parseInt(word))// NaN.
Enter fullscreen mode Exit fullscreen mode

Note, however, that the parseInt() function can take in two arguments; the string you are passing in and an optional radix.
A radix is a number between 2 and 36 which represents the base in a numeral system. For example, a radix of 2 would represent the binary system, while a radix of 10 represents the decimal system.

Converting a string to a number using the Math.floor() function

Using the let stringToNumber = "25", let's see the output of the Math.floor() method in code;

let stringToNumber = "25";
console.log(Math.floor(stringToNumber)); // number
Enter fullscreen mode Exit fullscreen mode

Math.floor() will return NaN using the let word = "I am a word" variable declaration.
Check it below;

let word = "I am a word"
console.log(Math.floor(word))// NaN
Enter fullscreen mode Exit fullscreen mode

Converting a string to a number using the Math.ceil() function

The Math.ceil function can also be used to convert a string to a number by rounding the number to the nearest integer if the number is a decimal.

Let's see in code;

let stringToNumber = "25.7"
console.log(Math.ceil(stringToNumber)) // 26 thus rounding the number to the nearest integer.
Enter fullscreen mode Exit fullscreen mode

Another example to show something

let stringToNumber = "25.1"
console.log(Math.ceil(stringToNumber)) // 26
Enter fullscreen mode Exit fullscreen mode

Math.ceil() will return NaN with our *word variable declaration.

let word = "I am a word"
console.log(Math.ceil(word))// NaN
Enter fullscreen mode Exit fullscreen mode

Thank you for reading.

Please don't forget to comment, like, and click here to share
.

To read about other methods of converting a string to a number in JavaScript, please read from the freeCodeCamp website.

Top comments (0)