Global Number() Function - Javascript Global

Javascript examples for Global:Number

Description

The Number() function converts the object argument to a number that represents the object's value.

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.

Parameter Values

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

Return Value:

A Number.

The following code shows how to Convert different object values to their numbers:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {//from w  ww .  j  av a  2  s .  c o m
    var x1 = true;
    var x2 = false;
    var x3 = new Date();
    var x4 = "999";
    var x5 = "999 888 234";

    var n =
    Number(x1) + "<br>" +
    Number(x2) + "<br>" +
    Number(x3) + "<br>" +
    Number(x4) + "<br>" +
    Number(x5);
    document.getElementById("demo").innerHTML = n;
}
</script>

</body>
</html>

Related Tutorials