The Number Object

The Number type has 18437736874454810627 values, representing the double-precision 64-bit format IEEE 754 values as specified in the IEEE Standard for Binary Floating-Point Arithmetic, except that the 9007199254740990 distinct 'Not-a-Number' values of the IEEE Standard are represented as a single special NaN value. There are two other special values, called positive Infinity and negative Infinity.

Number objects are full script objects. Number objects are created in two ways. The first way is by using the Number constructor (see below). The second way is by creating a number primitive and then giving it a property, as this automatically converts the primitive to an object.

You use a number literal to create a number primitive.

var myNumber = 1234;

A new number primitive is created that has the value 1234.

The number value of an object is derived as shown in the table below:

Object Type

Number Value

Undefined

NaN

Null

+0

Boolean primitive

The value is 1 if the boolean is true. The value is +0 if the boolean is false.

Number primitive

The value equals the input number (no conversion).

String primitive

The object is treated as a numeric literal and the value is derived from the literal. If the string is not a numeric literal then the value is +0.

Object

The object is converted to a primitive and this is converted to a number using the rules above.

Number objects are of class Number.

Number Object Properties

Property

Description

prototype

A reference to the Number Prototype. This property does not enumerate and can not be deleted but can be changed.

Number Object Functions

The Number object has no predefined functions.


The Number Constructor

Using the new Operator with the Number Constructor

The Number constructor is used to create new number objects. The constructor can have a single argument.

var myNumber = new Number(arg);

A new number object is created using the argument. If the argument is not a number literal then the new number will be the number value of the argument.

When a number object is created it is given the Number and String prototypes as well as the Boolean prototype. This allows you to treat the number as if it were a string or a boolean. Shown below is the Number object prototype chain.

Calling the Number Constructor as a Function

When the Number constructor is run as a function without the new operator it returns a number primitive that is the number resulting from converting the argument object to a number.

var myNumber = Number(arg);

A new number primitive is created using the argument. If the argument is not a number literal then the new number will be the number value (see above) of the argument.

Number Constructor Properties

Constructor properties are read only (they can not be changed or deleted) and it is not possible to add new properties to the Constructor.

Property

Description

MAX_VALUE

The number primitive Number.MAX_VALUE is the largest positive finite value of the number type, which is approximately 1.7976931348623157 × 10 to the power of 308. This property has does not enumerate and is read only.

MIN_VALUE

The number primitive Number.MIN_VALUE is the smallest positive value of the number type, which is approximately 5 × 10 to the power of -324 . This property has does not enumerate and is read only.

NaN

The number primitive Number.NaN. This property has does not enumerate and is read only.

NEGATIVE_INFINITY

The number primitive Number.NEGATIVE_INFINITY. This property has does not enumerate and is read only.

POSITIVE_INFINITY

The number primitive Number.POSiTIVE_INFINITY. This property has does not enumerate and is read only.

prototype

A reference to the Number Prototype. This property does not enumerate and can not be changed or deleted.

Number Constructor Functions

The Number constructor has no predefined functions.


The Number Prototype

This prototype can be used by any object that can be converted to a number value.

Number Prototype Properties

Property

Description

prototype

A reference to the String Prototype. This property does not enumerate and can not be deleted but can be changed.

Number Prototype Functions

The this reference used here always refers to the object that was used to run the function.

Except where explicitly stated otherwise all these functions are intentionally generic. They do not require that the this value or the arguments be Number objects, and can therefore be applied to other objects where they will use the number value (see above) of the object.

Function

Description

toFixed([fractionDigits])

Return a string primitive containing the number represented in fixed-point notation with fractionDigits digits after the decimal point. If fractionDigits is undefined, 0 is assumed.

fractionDigits must be in the range 0 to 20, any value outside this range will cause the returned value to be null.

Note: The output of toFixed may be more precise than toString for some values because toString only prints enough significant digits to distinguish the number from adjacent number values. For example, (1000000000000000128).toString() returns "1000000000000000100", while (1000000000000000128).toFixed(0) returns "1000000000000000128".

toExponential([fractionDigits])

Return a string primitive containing the number represented in exponential notation with one digit before the significand's decimal point and fractionDigits digits after the significand's decimal point. If fractionDigits is undefined, then as many significand digits as necessary to uniquely specify the number are included.

fractionDigits must be in the range 0 to 20, any value outside this range will cause the returned value to be null.

toLocaleString()

Returns the same as toString. This function is here so that in the future we can provide a Locale dependant version of toString.

toPrecision([precision])

Return a string primitive containing the number represented either in exponential notation with one digit before the significand's decimal point and precision–1 digits after the significand's decimal point or in fixed notation with precision significant digits. If precision is undefined then the returned value will be that defined by toString instead.

precision must be in the range 1 to 21, any value outside this range will cause the returned value to be null.

toString()

Returns a string representation of the number.

This function is not generic. If the this object is not a number object then it returns a string of the form "[object className]" where className is the class of this object.

valueOf()

Returns a number primitive derived from the number value of the object.

This function is not generic. If the this object is not a number object then this object is returned.

Comment on this topic

Topic ID: 150075