In this tutorial, we will introduce how to convert a string to integer, float and number in javascript.
1. Convert string to integer
We can use parseInt(string, radix) to convert a javascript string to an integer.
Here is an example:
parseInt("20") = 20 parseInt("30.00") = 30 parseInt("2.366") = 2 parseInt("3.14") = 3 parseInt("22 3 2019") = 22
2. Convert string to float
We can use parseFloat(string) to convert a string to a float number.
Here is an example:
parseFloat(" 10 ") = 10 parseFloat("123abc") = 123 parseFloat("abc456") = NaN parseFloat("3.14") = 3.14 parseFloat("18 2 2019") = 18
3. Convert string to number
We can use Number(string) function to convert a string to javascript number.
Here is an example:
Number(” 10 “) = 10 Number(“125.3”) = 125.3 Number(“abc456”) = NaN Number(“42px”) = NaN