JavaScript: The Definitive Guide

Previous Chapter 21
JavaScript Reference
Next
 

Number Object

Name

Number Object---place-holder for numeric constants

Availability

Navigator 3.0

Synopsis

Number.constant

Constructor

new Number(value)

Arguments

value

The numeric value of the Number object being created. This argument will be converted to a number, if necessary.

Returns

The newly constructed Number object

Constants

MAX_VALUE

The largest representable number.

MIN_VALUE

The smallest representable number.

NaN

Special Not-a-Number value.

NEGATIVE_INFINITY

Special negative infinite value; returned on overflow.

POSITIVE_INFINITY

Special infinite value; returned on overflow.

Methods

toString()

Convert a number to a string, using a specified radix (base).

valueOf()

Return the primitive numeric value contained by the Number object.

Description

Numbers are a basic, primitive data type in JavaScript. In Navigator 3.0, however, JavaScript also supports the Number object, an object type that represents a primitive numeric value. JavaScript automatically converts between the primitive and object forms as necessary. In Navigator 3.0, you can explicitly create a Number object with the Number() constructor, although there is rarely any need to do so.

The Number() constructor is actually more commonly used as a place-holder for five useful numeric constants: the largest and smallest representable numbers, positive and negative infinity, and the special Not-a-Number value. Note that these values are properties of the Number() constructor function itself, not of individual number objects. For example, you use the MAX_VALUE property as follows:

biggest = Number.MAX_VALUE
not like this:

n = new Number(2);
biggest = n.MAX_VALUE

By contrast, the toString() method of the Number object is a method of each Number object, not of the Number() constructor function. As noted above, JavaScript automatically converts from primitive numeric values to Number objects whenever necessary. This means that we can use the toString() method with a variable that holds a number, even though that value is not actually an object:

value = 1234;
binary_value = n.toString(2);

What happens in this code is that JavaScript implicitly invokes the Number() constructor to convert the number n to a temporary Number object for which the toString() method can be invoked. It is this toString() method that is the main reason for the existence of the Number object in the first place.

See Also

"Math", "Object"


Previous Home Next
netscape Book Index Number.MAX_VALUE

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell