Change Language


Follow Navioo On Twitter

Number : Object

Lets you work with numeric values. The Number object is an object wrapper for primitive numeric values.

Platform Support

IE Mozilla Netscape Opera Safari
3.0+ 1.0+ 3.0+ 7.0+ 1.0+

Constructors

Constructor Action IE Mozilla Netscape Opera Safari
Number Constructor(Number value) : Number
Creates a new instance of a Number.
Show Details 3.0+ 1.0+ 3.0+ 7.0+ 1.0+

Number(Number value) : Number

Creates a new instance of a Number.

Parameters

Number value The numeric value of the object being created.

Returns

Number

Properties

Property Action IE Mozilla Netscape Opera Safari
static MAX_VALUE : Number
The maximum numeric value representable in JavaScript.
Show Details 4.0+ 1.0+ 3.0+ 7.0+ 1.0+

Using MAX_VALUE

The following code multiplies two numeric values. If the result is less than or equal to MAX_VALUE, the func1 function is called; otherwise, the func2 function is called.

if (num1 * num2 <= Number.MAX_VALUE)
                        func1();
                        else
                        func2();
Remarks

The MAX_VALUE property has a value of approximately 1.79E+308. Values larger than MAX_VALUE are represented as "Infinity".

Because MAX_VALUE is a static property of Number, you always use it as Number.MAX_VALUE, rather than as a property of a Number object you created.

Availability

JavaScript 1.1|JScript 2.0, ECMAScript v1

static MIN_VALUE : Number
The smallest positive numeric value representable in JavaScript.
Show Details 4.0+ 1.0+ 3.0+ 7.0+ 1.0+

Using MIN_VALUE

The following code divides two numeric values. If the result is greater than or equal to MIN_VALUE, the func1 function is called; otherwise, the func2 function is called.

if (num1 / num2 >= Number.MIN_VALUE)
                        func1()
                        else
                        func2()
Remarks

The MIN_VALUE property is the number closest to 0, not the most negative number, that JavaScript can represent.

MIN_VALUE has a value of approximately 5e-324. Values smaller than MIN_VALUE ("underflow values") are converted to 0.

Because MIN_VALUE is a static property of Number, you always use it as Number.MIN_VALUE, rather than as a property of a Number object you created.

Availability

JavaScript 1.1|JScript 2.0, ECMAScript v1

static NaN : Number
A value representing Not-A-Number.
Show Details 4.0+ 1.0+ 3.0+ 7.0+ 1.0+

Using NaN

In the following example, if month has a value greater than 12, it is assigned NaN, and a message is displayed indicating valid values.

var month = 13
                        if (month < 1 || month > 12) {
                        month = Number.NaN
                        alert("Month must be between 1 and 12.")
                        }
Remarks

The value of Number.NaN is Not-A-Number, same as the value of global object's NaN property.

NaN is always unequal to any other number, including NaN itself; you cannot check for the not-a-number value by comparing to Number.NaN. Use the isNaN function instead.

Several JavaScript methods (such as the Number constructor, parseFloat, and parseInt) return NaN if the value specified in the parameter can not be parsed as a number.

You might use the NaN property to indicate an error condition for your function that returns a number in case of success.

JavaScript prints the value Number.NaN as NaN.

See Also

isNaN|Number.NaN|parseFloat|parseInt

Availability

JavaScript 1.1|JScript 2.0, ECMAScript v1

static NEGATIVE_INFINITY : Number
Special value representing negative infinity; returned on overflow.
Show Details 4.0+ 1.0+ 3.0+ 7.0+ 1.0+
See Also

Global.Infinity|isFinite

Availability

JavaScript 1.1|JScript 2.0, ECMAScript v1

static POSITIVE_INFINITY : Number
Special value representing infinity; returned on overflow.
Show Details 4.0+ 1.0+ 3.0+ 7.0+ 1.0+
See Also

Global.Infinity|isFinite

Availability

JavaScript 1.1|JScript 2.0, ECMAScript v1

constructor : Object
Specifies the function that creates the Number prototype.
Show Details 4.0+ 1.0+ 3.0+ 7.0+ 1.0+
See Also

Object.constructor

Availability

JavaScript 1.1

prototype : Object
Reference to the Number prototype object.
Show Details 4.0+ 1.0+ 3.0+ 7.0+ 1.0+
Availability

JavaScript 1.1

Functions

Method Action IE Mozilla Netscape Opera Safari
toExponential([Number digits]) : String
Returns a string representing the number in exponential notation.
Show Details 5.5+ 1.0+ 6.0+ 7.0+ 1.0+

Parameters

Number digits (optional)Number of decimal places to appear after the decimal point.

Returns

String
Throws
Throws RangeError if digits is too small or too large of a number.
Throws TypeError if the method is invoked on an object that is not a Number.
See Also

Number.toFixed|Number.toLocaleString|Number.toPrecision|Number.toString

Availability

JavaScript 1.5|JScript 5.5, ECMAScript v3

toFixed([Number digits]) : String
Returns a string representing the number in fixed-point notation.
Show Details 5.5+ 1.0+ 6.0+ 7.0+ 1.0+

Parameters

Number digits (optional)Number of decimal places to appear after the decimal point.

Returns

String
Throws
Throws RangeError if digits is too small or too large of a number.
Throws TypeError if the method is invoked on an object that is not a Number.
See Also

Number.toExponential|Number.toLocaleString|Number.toPrecision|Number.toString

Availability

JavaScript 1.5|JScript 5.5, ECMAScript v3

toLocaleString() : String
Returns a string representing the number that follows local formatting conventions.
Show Details 5.5+ 1.0+ 6.0+ 7.0+ 1.0+

Returns

String
Throws
Throws TypeError if the method is invoked on an object that is not a Number.
See Also

Number.toExponential|Number.toFixed|Number.toPrecision|Number.toString

Availability

JavaScript 1.5|JScript 5.5, ECMAScript v3

toPrecision([String precision]) : String
Returns a string representing the number to a specified precision in fixed-point notation.
Show Details 5.5+ 1.0+ 6.0+ 7.0+ 1.0+

Parameters

String precision (optional)The number of significant digits to use in the returned string.

Returns

String
Throws
Throws RangeError if digits is too small or too large of a number.
Throws TypeError if the method is invoked on an object that is not a Number.
See Also

Number.toExponential|Number.toFixed|Number.toLocaleString|Number.toString

Availability

JavaScript 1.5|JScript 5.5, ECMAScript v3

toSource() : Object
Returns an object literal representing the specified Number object; you can use this value to create a new object.
Show Details 4.0+ 1.0+ 3.0+ no no

Returns

Object
Availability

toString([Number radix]) : String
Returns a string representing the specified object.
Show Details 4.0+ 1.0+ 3.0+ 7.0+ 1.0+

Parameters

Number radix (optional)The radix (or base) between 2 and 36 to use to represent the number.

Returns

String
Throws
Throws TypeError if the method is invoked on an object that is not a Number.
See Also

Number.toExponential|Number.toFixed|Number.toLocaleString|Number.toPrecision

Availability

JavaScript 1.1|JScript 2.0, ECMAScript v1

valueOf() : Number
Returns the primitive value of the specified object.
Show Details 4.0+ 1.0+ 3.0+ 7.0+ 1.0+

Returns

Number
Throws
Throws TypeError if the method is invoked on an object that is not a Number.
See Also

Object.valueOf

Availability

JavaScript 1.1|JScript 2.0, ECMAScript v1

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:

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

Using Number object to modify all Number objects

The following example creates a Number object, myNum, then adds a description property to all Number objects. Then a value is assigned to the myNum object's description property.

myNum = new Number(65);
         Number.prototype.description = null;
         myNum.description = "wind speed";

Remarks

The primary uses for the Number object are:

  • To access its constant properties, which represent the largest and smallest representable numbers, positive and negative infinity, and the Not-a-Number value.
  • To create numeric objects that you can add properties to. Most likely, you will rarely need to create a Number object.

The properties of Number are properties of the class itself, not of individual Number objects.

JavaScript 1.2: Number(x) now produces NaN rather than an error if x is a string that does not contain a well-formed numeric literal. For example, the following prints NaN:

x=Number("three");
            document.write(x + "<BR>");

You can convert any object to a number using the top-level Number function.

References

Global.Infinity|Math|Global.NaN

Availability

JavaScript 1.1|JScript 2.0|ECMAScript v1

text_javascript Navioo_docs

Examples -> Source code Demo online - > number:

Ajax Javascript feed

↑ Grab this Headline Animator