What is Number() in JavaScript?
Definition
Number() takes a variable of any data type and converts it to a number.
Note:
- If the parameter cannot be converted to a number, then
NaNis returned.- If a date object is passed, then the number of seconds elapsed since January 1st, 1970 is returned.
- If more than one number is passed in a string separated by a space, then
NaNis returned, unlike theparseFloat()function.
Parameter
An optional variable of any data type, e.g., string, bool, date, etc.
Syntax
Number(value)
Return value
The input parameter converted to a number. If no parameter is provided, then 0 is returned.
Example
console.log( Number("105") )console.log( Number(true) )console.log( Number(false) )console.log( Number("10 20 30") )console.log( Number("abc") )console.log( Number(new Date()) )console.log( Number() )