NumberNN 3 IE 4 ECMA 1  

  

A Number object represents any numerical value, whether it is an integer or floating-point number. By and large, you don't have to worry about the Number object because a numerical value automatically becomes a Number object instance whenever you use such a value or assign it to a variable. On the other hand, you might want access to the static properties that only a math major would love.

 
Properties
 
constructorMAX_VALUEMIN_VALUENaN
NEGATIVE_INFINITYPOSITIVE_INFINITYprototype
 
Methods
 
toExponential( )toFixed( )toLocaleString( )toPrecision( )
toString( )valueOf( )
 
Creating a Number Object
 
var myValue = number;
var myValue = new Number(number);
constructorNN 4 IE 4 ECMA 1  

Read/Write  

This is a reference to the function that created the instance of a Number objectthe native Number( ) constructor function in browsers.

 
Example
 
if (myVar.constructor == Number) {
    // process native function
}
 
Value

Function object reference.

MAX_VALUENN 3 IE 4 ECMA 1  

Read-only  

Equal to the highest possible number that JavaScript can handle.

 
Example
 
var tiptop = Number.MAX_VALUE;
 
Value

1.7976931348623157e+308

MIN_VALUENN 3 IE 4 ECMA 1  

Read-only  

Equal to the smallest possible number that JavaScript can handle.

 
Example
 
var itsybitsy = Number.MIN_VALUE;
 
Value

5e-324

NaNNN 3 IE 4 ECMA 1  

Read-only  

Equal to a value that is not-a-number. JavaScript returns this value when a numerical operation yields a non-numerical result because of a flaw in one of the operands. If you want to test whether a value is not a number, use the isNaN( ) global function rather than comparing to this property value.

 
Value

NaN

NEGATIVE_INFINITY, POSITIVE_INFINITYNN 3 IE 4 ECMA 1  

Read-only  

Values that are outside of the bounds of Number.MIN_VALUE and Number.MAX_VALUE, respectively.

 
Example
 
Number.NEGATIVE_INFINITY
 
Value

-Infinity; Infinity

prototypeNN 3 IE 4 ECMA 1  

Read/Write  

A property of the static Number object. Use the prototype property to assign new properties and methods to future instances of a Number value created in the current document. See the Array.prototype property description for examples. There is little need to create new prototype properties or methods for the Number object.

 
Example
 
Number.prototype.author = "DG";
 
Value

Any data, including function references.

toExponential( )NN 6 IE 5.5(Win) ECMA 3  

toExponential(fractionDigits)

  

Returns a string containing the number object's value displayed in JavaScript's exponential notation. The single parameter specifies the number of digits to the right of the decimal to display in the string. For example, if a variable contains the number 9876.54, if you apply the toExponential(10) method, the result is 9.8765400000E+3, with zeroes padding the rightmost digits to reach a total of 10 digits to the right of the decimal. If you specify a parameter that yields a display with fewer digits than in the original number, the returned value is rounded.

 
Parameters
 
  • An integer specifying the number of digits to the right of the decimal in the returned string.
 
Returned Value

String.

toFixed( )NN 6 IE 5.5(Win) ECMA 3  

toFixed(fractionDigits)

  

Returns a string containing the number object's value displayed with a fixed number of digits to the right of the decimal (useful for currency calculation results). If you specify a parameter that yields a display with fewer significant digits than the original number, the returned value is rounded, but based only on the value of the digit immediately to the right of the last displayed digit (i.e., rounding does not cascade).

 
Parameters
 
  • An integer specifying the number of digits to the right of the decimal in the returned string.
 
Returned Value

String.

toLocaleString( )NN 6 IE 5(Mac/5.5(Win) ECMA 3  

  

Returns a string version of the number object's value. The precise format of the returned value is not mandated by the ECMA standard, and may be different from one local currency system to another (as set in the client computer's international preferences). On a U.S. English system, IE 5.5 and later for Windows returns a value with two digits to the right of the decimal (rounding values if necessary), with commas denoting thousands, millions, and so on. IE 5 for Macintosh does the same except for the commas. Netscape 6 performs no special formatting.

 
Parameters

None.

 
Returned Value

String.

toPrecision( )NN 6 IE 5.5(Win) ECMA 3  

toPrecision(precisionDigits)

  

Returns a string containing the number object's value displayed with a fixed number of digits, counting digits to the left and right of the decimal. If you specify a parameter that yields a display with fewer digits to the left of the decimal than the original number, the returned value is displayed in exponential notation. Truncated values are rounded, but based only on the value of the digit immediately to the right of the last displayed digit (i.e., rounding does not cascade).

 
Parameters
 
  • An integer specifying the total number of digits in the returned string.
 
Returned Value

String.

toString( )NN 4 IE 4 ECMA 1  

  

Returns the object's value as a string data type. You don't need this method in practice because the browsers automatically convert Number values to strings when they are needed for display in alert dialogs or in-document rendering.

 
Parameters

None.

 
Returned Value

String.

valueOf( )NN 4 IE 4 ECMA 1  

  

Returns the object's value.

 
Parameters

None.

 
Returned Value

A numeric value.