Visit Mozilla.org

Core JavaScript 1.5 Reference:Global Objects:Number

From MDC


Contents

[edit] Summary

Creates a wrapper object to allow you to work with numerical values.

[edit] Syntax

new Number(value)

[edit] Parameters

value
The numeric value of the object being created.

[edit] 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.

[edit] Properties

For properties inherited by Number instances, see Properties of Number instances.

MAX_VALUE
The largest representable number.
MIN_VALUE
The smallest representable number.
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.

Properties inherited from Function.prototype
caller, constructor, length, name

[edit] Methods

For methods inherited by Number instances, see Methods of Number instances.

Although the Number object contains no methods of its own, it does inherit some methods through the prototype chain.

Methods inherited from Function.prototype
apply, call, toSource, toString, valueOf

Methods inherited from Object.prototype
__defineGetter__, __defineSetter__, hasOwnProperty, isPrototypeOf, __lookupGetter__, __lookupSetter__, __noSuchMethod__, propertyIsEnumerable, unwatch, watch

[edit] Number instances

All Number instances inherit from Number.prototype. The prototype object of the Number constructor can be modified to affect all Number instances.

[edit] Properties

constructor
Returns the function that created this object's instance. By default this is the Number object.

[edit] Methods

toExponential
Returns a string representing the number in exponential notation.
toFixed
Returns a string representing the number in fixed-point notation.
toLocaleString
Returns a human readable string representing the number using the locale of the environment. Overrides the Object.prototype.toLocaleString method.
toPrecision
Returns a string representing the number to a specified precision in fixed-point or exponential notation.
toSource
Non-standard
Returns an object literal representing the specified Number object; you can use this value to create a new object. Overrides the Object.prototype.toSource method.
toString
Returns a string representing the specified object. Overrides the Object.prototype.toString method.
valueOf
Returns the primitive value of the specified object. Overrides the Object.prototype.valueOf method.

Methods inherited from Object.prototype
__defineGetter__, __defineSetter__, hasOwnProperty, isPrototypeOf, __lookupGetter__, __lookupSetter__, __noSuchMethod__, propertyIsEnumerable, unwatch, watch

[edit] Examples

[edit] 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:

biggestNum = Number.MAX_VALUE;
smallestNum = Number.MIN_VALUE;
infiniteNum = Number.POSITIVE_INFINITY;
negInfiniteNum = Number.NEGATIVE_INFINITY;
notANum = Number.NaN;

[edit] 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".

[edit] See also