JavaScript Number()
Examples
Convert booleans and dates to a number:
Number(true);
Number(false);
Number(new Date());
Try it Yourself »
More examples below.
Description
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 (JavaScript 1997) feature.
It is supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
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 »