JavaScript Number()
Examples
Convert booleans and dates to a number:
Number(true);
Number(false);
Number(new Date());
Try it Yourself »
More examples below.
Definition and Usage
The Number()
method converts a value to a number.
If the value cannot be converted, NaN
is returned.
Notes
For booleans, Number() returns 0 or 1.
For dates, Number() returns milliseconds since January 1, 1970 00:00:00.
For strings, Number() returns a number or NaN.
Syntax
Number(value)
Parameters
Parameter | Description |
value | Optional. A JavaScript value (variable). |
Return Value
Type | Description |
A number | Returns the value as a number. If the value cannot be converted to a number, NaN is returned. If no value is provided, 0 is returned. |
Browser Support
Number()
is an ECMAScript1 (ES1) feature.
ES1 (JavaScript 1997) is fully supported in all browsers:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |
More Examples
Convert different numbers to a number:
Number(999);
Number("999");
Number("999 888");
Try it Yourself »
Convert different arrays to a number:
Number([9]);
Number([9.9]);
Number([9,9]);
Try it Yourself »