JavaScript » Objects » Number

Syntax:
new Number(value)

The Number object is an object wrapper for primitive numeric values, allowing for their manipulation. To create a Number object use Number constructor above.

Examples

Code:
five = new Number(5)
Explanation:

This following example creates a Number object of the numeric value 5. The main reason for doing this is to be able to use the constant properties for the Number object, although you can create one in order to add properties to it. You can also convert any object to a number by using the Number function.

Properties

constructor

Syntax: object.constructor

This property specifies the function that created the object's prototype. See also the Object.constructor property.

MAX_VALUE

Syntax: Number.MAX_VALUE

This property represents the largest value possible in JavaScript. It is a static property and hence always referred to as Number.MAX_VALUE, and has a value of approximately 1.79769e+308. Numbers larger than this are represented as infinity.

MIN_VALUE

Syntax: Number.MIN_VALUE

This property represents the smallest positive number possible in JavaScript, and as a static property is always referred to as Number.MIN_VALUE. Its value is 5e-324, and any value smaller than that is converted to 0.

NaN

Syntax: Number.NaN

This read-only property represents the special value Not-a-Number, and is always unequal to any other number (including 0) and to NaN itself. As a static property, it is always referred to as Number.NaN.

NEGATIVE_INFINITY

Syntax: Number.NEGATIVE_INFINITY

This static, read-only property is a special value representing negative infinity, which is returned on overflow.

prototype

Syntax: object.prototype

This property represents the prototype for this object and allows you to add methods and properties of your own. See also the Function.prototype property.

Methods

toExponential

Syntax: object.toExponential(fractionDigits)

This method returns a string containing the number in exponential form.

toFixed

Syntax: object.toFixed(fractionDigits)

This method returns a string containing the number represented in fixed-point notation with fractionDigits following the decimal point.

toString

Syntax: object.toString([radix])

This method returns a string representing the Number object, and is called by JavaScript whenever the code requires a string value. The optional 'radix' parameter is an integer between 2 and 36 which specifies the base to be used when representing numeric values. This method overrides the Object.toString method.

valueOf

Syntax: object.valueOf()
 
NOTE:
 
The Number object also inherits the watch and unwatch methods from the Object object.

This method, which is usually called by Javascript behind the scenes, returns the primitive value of a Number object as a number data type. This method overrides the Object.valueOf method.

See Also: