Number

Summary

The Number JavaScript object is a wrapper object allowing you to work with numerical values. A Number object is created using the Number() constructor.

Syntax

new Number(value)

Parameters

value
The numeric value of the object being created.

Description

The primary uses for the Number object are:

If the argument cannot be converted into a number, it returns NaN.

In a non-constructor context (i.e., without the new operator), Number can be used to perform a type conversion.

Properties

For properties available on Number instances, see Properties of Number instances.
EPSILON
The smallest interval between two representable numbers.
MAX_VALUE
The largest positive representable number. The largest negative representable number is -MAX_VALUE.
MIN_VALUE
The smallest positive representable number--that is, the positive number closest to zero (without actually being zero). The smallest negative representable number is -MIN_VALUE.
NaN
Special "not a number" value.
NEGATIVE_INFINITY
Special value representing negative infinity; returned on overflow.
POSITIVE_INFINITY
Special value representing infinity; returned on overflow.
prototype
Allows the addition of properties to a Number object.
Properties inherited from Function:

Methods

For methods available on Number instances, see Methods of Number instances.
isNaN
Determine whether the passed value is NaN.
isFinite
Determine whether the type of and the passed value itself is a finite number.
isInteger
Determine whether the type of the passed value is "number" and the passed value is an integer.
toInteger
Evaluate the passed value and convert it to an integer (or Infinity).
Methods inherited from Function:

Number instances

All Number instances inherit from Number.prototype. The prototype object of the Number constructor can be modified to affect all Number instances.

Properties

constructor
Returns the function that created this object's instance. By default this is the Number object.
Properties inherited from Object:

Methods

Number.prototype.toExponential()
Returns a string representing the number in exponential notation.
Number.prototype.toFixed()
Returns a string representing the number in fixed-point notation.
Number.prototype.toLocaleString()
Returns a string with a language sensitive representation of this number. Overrides the Object.prototype.toLocaleString() method.
Number.prototype.toPrecision()
Returns a string representing the number to a specified precision in fixed-point or exponential notation.
Number.prototype.toSource()
Returns an object literal representing the specified Number object; you can use this value to create a new object. Overrides the Object.prototype.toSource() method.
Number.prototype.toString()
Returns a string representing the specified object. Overrides the Object.prototype.toString() method.
Number.prototype.valueOf()
Returns the primitive value of the specified object. Overrides the Object.prototype.valueOf() method.
 

Examples

Example: 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:

var biggestNum = Number.MAX_VALUE;
var smallestNum = Number.MIN_VALUE;
var infiniteNum = Number.POSITIVE_INFINITY;
var negInfiniteNum = Number.NEGATIVE_INFINITY;
var notANum = Number.NaN;

Example: Integer range for Number

The following example shows minimum and maximum integer values that can be represented as Number object (for details, refer to EcmaScript standard, chapter 8.5 The Number Type):

var biggestInt = 9007199254740992;
var smallestInt = -9007199254740992;

When parsing data that has been serialized to JSON, integer values falling out of this range can be expected to become corrupted when JSON parser coerces them to Number type. Using String instead is a possible workaround.

 Example: Using Number to convert a Date object

The following example converts the Date object to a numerical value using Number as a function:

var d = new Date("December 17, 1995 03:24:00");
print(Number(d));

This displays "819199440000".

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support (Yes) (Yes) (Yes) (Yes) (Yes)
toSource() Not supported (Yes) Not supported Not supported Not supported
isNaN() 25.0 15 (15) Not supported Not supported Not supported
isFinite() 25.0 16 (16) Not supported Not supported Not supported
isInteger() Not supported 16 (16) Not supported Not supported Not supported
toInteger() Not supported 16 (16) Not supported Not supported Not supported
EPSILON Not supported 25 (25) Not supported Not supported Not supported
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support (Yes) (Yes) (Yes) (Yes) (Yes)
toSource() Not supported (Yes) Not supported Not supported Not supported
isNaN() Not supported 15.0 (15) Not supported Not supported Not supported
isFinite() Not supported 16.0 (16) Not supported Not supported Not supported
isInteger() Not supported 16.0 (16) Not supported Not supported Not supported
toInteger() Not supported 16.0 (16) Not supported Not supported Not supported
EPSILON Not supported 25.0 (25) Not supported Not supported Not supported

See also

 

Document Tags and Contributors

Contributors to this page: rajat_agarwal
Last updated by: rajat_agarwal,