Sometimes, we want to convert string to number in JavaScript.
In this article, we’ll look at how to convert string to number in JavaScript.
How to convert string to number in JavaScript?
To convert string to number in JavaScript, we can use the Number, parseInt, parseFloat functions, or the unary + operator.
For instance, we write
const num = Number(x);
or
const num = parseInt(x, 10);
or
const num = parseFloat(x);
or
const num = +x;
to convert string x to a number with the Number, parseInt, parseFloat functions, or the unary + operator.
Unary +, Number and parseFloat convert x to an floating point number.
And parseInt converts x to an integer.
parseInt takes the radix as a 2nd argument. 10 means we parse x into a decimal number.
Conclusion
To convert string to number in JavaScript, we can use the Number, parseInt, parseFloat functions, or the unary + operator