How to convert strings to numbers with vanilla JS (and back again)

Today, we’re going to look at three methods you can use to convert a string into a number, and one method for converting a number into a string.
Let’s dig in.
parseInt() You can use the parseInt() method to convert a string into an integer (a whole number), or turn a decimal into an integer.
Pass in the string (or number) as the first argument. The second argument is called the radix.


This content originally appeared on Go Make Things and was authored by Go Make Things

Today, we’re going to look at three methods you can use to convert a string into a number, and one method for converting a number into a string.

Let’s dig in.

parseInt()

You can use the parseInt() method to convert a string into an integer (a whole number), or turn a decimal into an integer.

Pass in the string (or number) as the first argument. The second argument is called the radix. This is the base number used in mathematical systems. For our use, it should always be 10.

// returns 42
parseInt('42', 10);

// also returns 42
parseInt('42px', 10);

// ALSO also returns 42
parseInt(42.1234, 10);

Here’s a demo of the parseInt() method.

parseFloat()

You can use the parseFloat() method to convert a string into a point number (a number with decimal points). Pass in the string as an argument.

If the number has no decimal places, an integer is returned.

// returns 3.14
parseFloat('3.14');

// also returns 3.14
parseFloat('3.14someRandomStuff');

// returns 3
parseFloat('3');

Here’s a demo of the parseFloat() method.

Number()

You can also use the Number() method to convert a string into a number.

Sometimes it returns integer. Other times it returns a float. And if you pass in a string with random text in it, you’ll get NaN, an acronym for Not a Number.

As a result of this inconsistency, I personally prefer to use parseInt() or parseFloat(). If you know the format of the number you’d like, use those instead. If you want the string to fail with NaN if it has other characters in it, though, Number() may actually be a better choice.

// returns 123
Number('123');

// returns 12.3
Number('12.3');

// returns NaN
Number('3.14someRandomStuff');

Here’s a demo of the Number() method.

Number.toString()

You can use the Number.toString() method to convert a number into a string.

let pi = 3.14;
let eleven = 11;

// returns "3.14"
pi.toString();

// returns "11"
eleven.toString();

Here’s a demo of the Number.toString() method.


This content originally appeared on Go Make Things and was authored by Go Make Things


Print Share Comment Cite Upload Translate Updates
APA

Go Make Things | Sciencx (2021-06-08T14:30:00+00:00) How to convert strings to numbers with vanilla JS (and back again). Retrieved from https://www.scien.cx/2021/06/08/how-to-convert-strings-to-numbers-with-vanilla-js-and-back-again/

MLA
" » How to convert strings to numbers with vanilla JS (and back again)." Go Make Things | Sciencx - Tuesday June 8, 2021, https://www.scien.cx/2021/06/08/how-to-convert-strings-to-numbers-with-vanilla-js-and-back-again/
HARVARD
Go Make Things | Sciencx Tuesday June 8, 2021 » How to convert strings to numbers with vanilla JS (and back again)., viewed ,<https://www.scien.cx/2021/06/08/how-to-convert-strings-to-numbers-with-vanilla-js-and-back-again/>
VANCOUVER
Go Make Things | Sciencx - » How to convert strings to numbers with vanilla JS (and back again). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/08/how-to-convert-strings-to-numbers-with-vanilla-js-and-back-again/
CHICAGO
" » How to convert strings to numbers with vanilla JS (and back again)." Go Make Things | Sciencx - Accessed . https://www.scien.cx/2021/06/08/how-to-convert-strings-to-numbers-with-vanilla-js-and-back-again/
IEEE
" » How to convert strings to numbers with vanilla JS (and back again)." Go Make Things | Sciencx [Online]. Available: https://www.scien.cx/2021/06/08/how-to-convert-strings-to-numbers-with-vanilla-js-and-back-again/. [Accessed: ]
rf:citation
» How to convert strings to numbers with vanilla JS (and back again) | Go Make Things | Sciencx | https://www.scien.cx/2021/06/08/how-to-convert-strings-to-numbers-with-vanilla-js-and-back-again/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.