Javascript - Global Number() Function

The Number() function converts the object to a number.

Description

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.

Syntax

Number(object)

Parameter Values

Parameter Require Description
objectOptional.A JavaScript object. If no argument is provided, it returns 0.

Return

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.

Example

Convert different object values to their numbers:

Demo

//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);

Result