Loading lesson path
JavaScript variables can be converted to a new variable and another data type:
converts a variable (or a value) into a number. A numeric string (like "3.14") converts to a number (like 3.14). An empty string (like "") converts to 0. A non numeric string (like "John") converts to
(Not a Number).
Number("3.14") Number(Math.PI) Number(" ") Number("")
Number("99 88") Number("John")
Number Methods, you will find more methods that can be used to convert strings to numbers:
Returns a number, converted from its argument parseFloat() Parses a string and returns a floating point number parseInt()
Formula
The Unary + Operator
The unary + operator can be used to convert a variable to a number:let y = "5";
// y is a string let x = + y;// x is a number If the variable cannot be converted, it will still become a number, but with the value
(Not a Number):