JavaScript Number()Function

函数参考手册JavaScript Global Functions

Definition and Usage

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

If the value of the object cannot be converted to a number, the Number() function returns NaN.

Syntax

Number(object)

Parameters Description
object Optional. A JavaScript object. If no parameter is provided, it returns 0.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the Number() function


Tips and Notes

Note:If the parameter is a Date object, Number() returns the number of milliseconds from January 1, 1970 to the present.


Examples

Examples

Convert different objects to numbers:

<script>

var test1= new Boolean(true);
var test2= new Boolean(false);
var test3= new Date();
var test4= new String("999");
var test5= new String("999 888");

document.write(Number(test1)+ "<br>");
document.write(Number(test2)+ "<br>");
document.write(Number(test3)+ "<br>");
document.write(Number(test4)+ "<br>");
document.write(Number(test5)+ "<br>");

</script>

The output result of the above example:


Try it Yourself »


函数参考手册JavaScript Global Functions Other Extensions