Lets you work with numeric values. The Number object is an object wrapper
for primitive numeric values.
Platform Support
IE | Mozilla | Netscape | Opera | Safari |
---|---|---|---|---|
3.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
Constructors
Constructor | Action | IE | Mozilla | Netscape | Opera | Safari |
---|---|---|---|---|---|---|
Number Constructor(Number value) : Number
Creates a new instance of a Number.
|
Show Details | 3.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
Properties
Property | Action | IE | Mozilla | Netscape | Opera | Safari |
---|---|---|---|---|---|---|
static MAX_VALUE : Number
The maximum numeric value representable in JavaScript.
|
Show Details | 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
static MIN_VALUE : Number
The smallest positive numeric value representable in JavaScript.
|
Show Details | 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
static NaN : Number
A value representing Not-A-Number.
|
Show Details | 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
static NEGATIVE_INFINITY : Number
Special value representing negative infinity; returned on overflow.
|
Show Details | 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
static POSITIVE_INFINITY : Number
Special value representing infinity; returned on overflow.
|
Show Details | 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
constructor : Object
Specifies the function that creates the Number prototype.
|
Show Details | 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
prototype : Object
Reference to the Number prototype object.
|
Show Details | 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
Functions
Method | Action | IE | Mozilla | Netscape | Opera | Safari |
---|---|---|---|---|---|---|
toExponential([Number digits]) : String
Returns a string representing the number in exponential notation.
|
Show Details | 5.5+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ |
toFixed([Number digits]) : String
Returns a string representing the number in fixed-point notation.
|
Show Details | 5.5+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ |
toLocaleString() : String
Returns a string representing the number that follows local formatting
conventions.
|
Show Details | 5.5+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ |
toPrecision([String precision]) : String
Returns a string representing the number to a specified precision in fixed-point
notation.
|
Show Details | 5.5+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ |
toSource() : Object
Returns an object literal representing the specified Number object; you
can use this value to create a new object.
|
Show Details | 4.0+ | 1.0+ | 3.0+ | no | no |
toString([Number radix]) : String
Returns a string representing the specified object.
|
Show Details | 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
valueOf() : Number
Returns the primitive value of the specified object.
|
Show Details | 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
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;
Using Number
object to modify all Number
objects
The following example creates a Number object, myNum, then adds a description property to all Number objects. Then a value is assigned to the myNum object's description property.
myNum = new Number(65); Number.prototype.description = null; myNum.description = "wind speed";
Remarks
The primary uses for the Number
object are:
- To access its constant properties, which represent the largest and smallest representable numbers, positive and negative infinity, and the Not-a-Number value.
- To create numeric objects that you can add properties to. Most likely,
you will rarely need to create a
Number
object.
The properties of Number
are properties of the class itself, not of individual Number
objects.
JavaScript 1.2: Number(x)
now produces NaN
rather than an error if x
is a string that does not contain a well-formed numeric literal. For example,
the following prints NaN
:
x=Number("three"); document.write(x + "<BR>");
You can convert any object to a number using the top-level Number function.
References
Global.Infinity|Math|Global.NaN
Availability
JavaScript 1.1|JScript 2.0|ECMAScript v1