How to convert string to number in JavaScript
To convert a string to a number, we can use the unary operator plus +
in JavaScript.
Here is an example:
const string = "4";
console.log(+string); // 4
Similarly, we can also the Number()
method in JavaScript to convert a string to number.
const string = "4";
console.log(Number(string)); // 4
The Number()
method takes the string number as an argument and converts it to a number.