The Number() function converts the object to a number.
The Number() function converts the object to a number.
If the value cannot be converted to a legal number, NaN is returned.
If the parameter is a Date object, the Number() function returns the number of milliseconds since midnight January 1, 1970 UTC.
Number(object)
Parameter | Require | Description |
---|---|---|
object | Optional. | A JavaScript object. If no argument is provided, it returns 0. |
A Number. Returns different object values to their numbers.
If the value cannot be converted to a legal number, NaN is returned.
If no argument is provided, it returns 0.
Convert different object values to their numbers:
//convert variables to numbers. var x1 = true;/* w w w . ja va 2 s. co m*/ var x2 = false; var x3 = new Date(); var x4 = "999"; var x5 = "999 888"; var n = Number(x1) + "\n" + Number(x2) + "\n" + Number(x3) + "\n" + Number(x4) + "\n" + Number(x5); console.log(n);