Browser/User Agent Support
| IE | Mozilla | Netscape | Opera | Safari | 3.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
|---|
Constructors
| Constructor | IE | Mozilla | Netscape | Opera | Safari |
|---|---|---|---|---|---|
Creates a new instance of a Number. | 3.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
Properties
| Property | IE | Mozilla | Netscape | Opera | Safari |
|---|---|---|---|---|---|
The maximum numeric value representable in
JavaScript.
| 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
The smallest positive numeric value representable in
JavaScript.
| 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
A value representing Not-A-Number. | 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
Special value representing negative infinity; returned on overflow. | 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
Special value representing infinity; returned on overflow. | 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
Specifies the function that creates the Number prototype. | 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
Reference to the Number prototype object. | 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
Methods
| Method | IE | Mozilla | Netscape | Opera | Safari |
|---|---|---|---|---|---|
Returns a string representing the number in exponential notation. | 5.5+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ |
Returns a string representing the number in fixed-point notation. | 5.5+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ |
Returns a string representing the number that follows local formatting conventions. | 5.5+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ |
Returns a string representing the number to a specified precision in fixed-point notation. | 5.5+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ |
Returns an object literal representing the specified Number object; you can use this value to create a new object. | 4.0+ | 1.0+ | 3.0+ | no | no |
Returns a string representing the specified object. | 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
Returns the primitive value of the specified object. | 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
Numberobject.
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
Constructor Detail
Number Number(Number value)
Creates a new instance of a Number.
| Number | value | The numeric value of the object being created. |
Property Detail
static Number MAX_VALUE - read only
The maximum numeric value representable in JavaScript.
-
Using
MAX_VALUEThe following code multiplies two numeric values. If the result is less than or equal to
MAX_VALUE, thefunc1function is called; otherwise, thefunc2function is called.if (num1 * num2 <= Number.MAX_VALUE) func1(); else func2();
- Remarks
-
The
MAX_VALUEproperty has a value of approximately 1.79E+308. Values larger thanMAX_VALUEare represented as "Infinity".Because
MAX_VALUEis a static property ofNumber, you always use it asNumber.MAX_VALUE, rather than as a property of aNumberobject you created. - Availability
JavaScript 1.1 | JScript 2.0, ECMAScript v1
static Number MIN_VALUE - read only
The smallest positive numeric value representable in JavaScript.
-
Using
MIN_VALUEThe following code divides two numeric values. If the result is greater than or equal to
MIN_VALUE, thefunc1function is called; otherwise, thefunc2function is called.if (num1 / num2 >= Number.MIN_VALUE) func1() else func2()
- Remarks
-
The
MIN_VALUEproperty is the number closest to 0, not the most negative number, that JavaScript can represent.MIN_VALUEhas a value of approximately 5e-324. Values smaller thanMIN_VALUE("underflow values") are converted to 0.Because
MIN_VALUEis a static property ofNumber, you always use it asNumber.MIN_VALUE, rather than as a property of aNumberobject you created. - Availability
JavaScript 1.1 | JScript 2.0, ECMAScript v1
static Number NaN - read only
A value representing Not-A-Number.
-
Using
NaNIn the following example, if month has a value greater than 12, it is assigned
NaN, and a message is displayed indicating valid values.var month = 13 if (month < 1 || month > 12) { month = Number.NaN alert("Month must be between 1 and 12.") } - Remarks
-
The value of
Number.NaNis Not-A-Number, same as the value of global object's NaN property.NaNis always unequal to any other number, includingNaNitself; you cannot check for the not-a-number value by comparing toNumber.NaN. Use theisNaNfunction instead.Several JavaScript methods (such as the
Numberconstructor,parseFloat, andparseInt) returnNaNif the value specified in the parameter can not be parsed as a number.You might use the
NaNproperty to indicate an error condition for your function that returns a number in case of success.JavaScript prints the value
Number.NaNasNaN. - See Also
isNaN | Number.NaN | parseFloat | parseInt
- Availability
JavaScript 1.1 | JScript 2.0, ECMAScript v1
static Number NEGATIVE_INFINITY - read only
Special value representing negative infinity; returned on overflow.
- See Also
- Availability
JavaScript 1.1 | JScript 2.0, ECMAScript v1
static Number POSITIVE_INFINITY - read only
Special value representing infinity; returned on overflow.
- See Also
- Availability
JavaScript 1.1 | JScript 2.0, ECMAScript v1
Object constructor - only
Specifies the function that creates the Number prototype.
- See Also
- Availability
JavaScript 1.1
Object prototype - only
Reference to the Number prototype object.
- Availability
JavaScript 1.1
Method Detail
toExponential([Number digits]) : String
Returns a string representing the number in exponential notation.
| Number | digits | Number of decimal places to appear after the decimal point. (optional) |
- Throws
- Throws RangeError if digits is too small or too large of a number.
- Throws TypeError if the method is invoked on an object that is not a Number.
- See Also
Number.toFixed | Number.toLocaleString | Number.toPrecision | Number.toString
- Availability
JavaScript 1.5 | JScript 5.5, ECMAScript v3
toFixed([Number digits]) : String
Returns a string representing the number in fixed-point notation.
| Number | digits | Number of decimal places to appear after the decimal point. (optional) |
- Throws
- Throws RangeError if digits is too small or too large of a number.
- Throws TypeError if the method is invoked on an object that is not a Number.
- See Also
Number.toExponential | Number.toLocaleString | Number.toPrecision | Number.toString
- Availability
JavaScript 1.5 | JScript 5.5, ECMAScript v3
toLocaleString() : String
Returns a string representing the number that follows local formatting conventions.
- Throws
- Throws TypeError if the method is invoked on an object that is not a Number.
- See Also
Number.toExponential | Number.toFixed | Number.toPrecision | Number.toString
- Availability
JavaScript 1.5 | JScript 5.5, ECMAScript v3
toPrecision([String precision]) : String
Returns a string representing the number to a specified precision in fixed-point notation.
| String | precision | The number of significant digits to use in the returned string. (optional) |
- Throws
- Throws RangeError if digits is too small or too large of a number.
- Throws TypeError if the method is invoked on an object that is not a Number.
- See Also
Number.toExponential | Number.toFixed | Number.toLocaleString | Number.toString
- Availability
JavaScript 1.5 | JScript 5.5, ECMAScript v3
toSource() : Object
Returns an object literal representing the specified Number object; you can use this value to create a new object.
- Availability
toString([Number radix]) : String
Returns a string representing the specified object.
| Number | radix | The radix (or base) between 2 and 36 to use to represent the number. (optional) |
- Throws
- Throws TypeError if the method is invoked on an object that is not a Number.
- See Also
Number.toExponential | Number.toFixed | Number.toLocaleString | Number.toPrecision
- Availability
JavaScript 1.1 | JScript 2.0, ECMAScript v1
valueOf() : Number
Returns the primitive value of the specified object.
- Throws
- Throws TypeError if the method is invoked on an object that is not a Number.
- See Also
- Availability
JavaScript 1.1 | JScript 2.0, ECMAScript v1
