JavaScript Number() function
- Previous Page NaN
- Next Page parseFloat()
- Go to the Previous Level JavaScript Global Reference Manual
Definition and usage
Number()
The function converts the object parameter to a number representing the object's value.
If the value cannot be converted to a valid number, it returns NaN.
Note:If the parameter is a Date object, then Number()
The function returns the number of milliseconds since midnight on January 1, 1970, in UTC.
Example
Convert different object values to numbers:
var x1 = true; var x2 = false; var x3 = new Date(); var x4 = "999"; var x5 = "999 888"; var n = Number(x1) + "<br>" + Number(x2) + "<br>" + Number(x3) + "<br>" + Number(x4) + "<br>" + Number(x5);
Syntax
Number(object)
Parameter value
Parameter | Description |
---|---|
object | Optional. JavaScript object. If this parameter is not provided, it returns 0. |
Technical details
Return value: | Number. Returns different object values as numbers. If the value cannot be converted to a valid number, it returns NaN. If no parameter is provided, it returns 0. |
---|---|
JavaScript Version: | ECMAScript 1 |
Browser Support
Function | Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Number() | Support | Support | Support | Support | Support |
- Previous Page NaN
- Next Page parseFloat()
- Go to the Previous Level JavaScript Global Reference Manual