Summary
Creates a wrapper object to allow you to work with numerical values.
Syntax
new Number(value)
Parameters
-
value - The numeric value of the object being created.
Description
The primary uses for the Number object are:
If the argument cannot be converted into a number, it returns NaN.
In a non-constructor context (i.e., without the new operator), Number can be used to perform a type conversion.
Properties
For properties available on Number instances, see Properties of Number instances.
The largest positive representable number. The largest negative representable number is -MAX_VALUE.
The smallest positive representable number -- that is, the positive number closest to zero (without actually being zero). The smallest negative representable number is -MIN_VALUE.
- NaN
- Special "not a number" value.
- NEGATIVE_INFINITY
- Special value representing negative infinity; returned on overflow.
- POSITIVE_INFINITY
- Special value representing infinity; returned on overflow.
- prototype
- Allows the addition of properties to a Number object.
Methods
For methods available on Number instances, see Methods of Number instances.
-
isNaN -
New in Firefox 15
Determine whether the passed value is NaN.
Number instances
All Number instances inherit from Number.prototype. The prototype object of the Number constructor can be modified to affect all Number instances.
Properties
- constructor
-
Returns the function that created this object's instance. By default this is the
Numberobject.
Methods
- constructor
-
Returns the function that created this object's instance. By default this is the
Numberobject.
Examples
Example: Using the Number object to assign values to numeric variables
The following example uses the Number object's properties to assign values to several numeric variables:
var biggestNum = Number.MAX_VALUE; var smallestNum = Number.MIN_VALUE; var infiniteNum = Number.POSITIVE_INFINITY; var negInfiniteNum = Number.NEGATIVE_INFINITY; var notANum = Number.NaN;
Example: Using Number to convert a Date object
The following example converts the Date object to a numerical value using Number as a function:
var d = new Date("December 17, 1995 03:24:00");
print(Number(d));
This displays "819199440000".
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
| toSource() | -- | (Yes) | -- | -- | -- |
| Feature | Android | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
| toSource() | -- | (Yes) | -- | -- | -- |
Mozilla Developer Network