The Number object supports numeric constants and methods to help work with numbers.
In JavaScript, primitive numeric values (e.g. 1, 2, 23.12, -2.34) differ from the Number object, their data type is number not object.
When you use numbers in JavaScript, you work with primitive numeric values, not instances of the Number object.
The JavaScript interpreter does not use the Number object for implicit data type conversions (when a non-numeric value needs to be converted to numeric, for example 2 * "32"), but the Number object can be used as a function to convert a value to a primitive numeric value explicitly.
See Example 3 for details.
When the JavaScript interpreter needs to convert a primitive numeric value to an object, it uses the Number object.
Example 4 illustrates this mechanism.
If number cannot be converted into a numeric value, num will be NaN.
Creating primitive numeric values:
In JavaScript, integers can be represented in decimal, hexadecimal and octal formats, while floating point numbers and numbers in exponential notation must be in decimal format.
Hexadecimal integers begin with 0x or 0X followed by one or more hexadecimal digits (0–9, A–F, a-f).
Octal integers begin with 0 followed by one or more octal digits (0-7). If an integer begins with 0 and contains the digit 8 or 9, it is a decimal number.
/* decimal formats: */var num = 12; // integer
var num = 12.23; // floating point number
var num = -12.23; // negative number
var num = .23; // 0.23
var num = -.23; // -0.23
var num = 12e3; // exponential notation (12000)
var num = 12e+3; // exponential notation (12000)
var num = 12e-3; // exponential notation (0.012)
var num = 0.012e+3; // floating point + exponential (12)
/* hexadecimal formats: */var num = 0xFF; // value (FF) in hexadecimal format
var num = 0Xaf9857; // another value (af9857) in hexadecimal format
var num = -0xff; // negative value (-ff) in hexadecimal format
/* not allowed hexadecimal formats: */var num = 0xff.12; // floating point number
var num = 0xffe+3; // it is allowed, but means 0xffe + 3, not 0xffe * 1000
/* octal formats: */var num = 0734; // value (734) in octal format
var num = -023456; // negative value (-23456) in octal format
/* decimal instead of octal */var num = 0784; // value (784) in decimal format since 8 is not an octal digit
/* not allowed octal formats: */var num = 0734.12; // floating point number
var num = 0734e+3; // exponential notation
The Number object inherits from the Number.prototype and Function.prototype objects.
The following lists only contain the members of the Number and Number.prototype objects.
Returns the largest possible number that JavaScript can represent. The MAX_VALUE property is static, it cannot be accessed from an instance of the Number object, only Number.MAX_VALUE is allowed.
Be careful, it does not mean that JavaScript can work with numbers between (-1 * Number.MAX_VALUE, Number.MAX_VALUE) correctly. In a 32-bit system the real limit is (-1 * 2^53, 2^53). See the Infinity global property for details and examples.
Returns the smallest positive floating point number, that JavaScript can represent (about 2^-1074). The MIN_VALUE property is static, it cannot be accessed from an instance of the Number object, only Number.MIN_VALUE is allowed.
Same as the global NaN property.
A constant value that indicates that the value of an expression is 'Not-A-Number'.
The NaN property is static, it cannot be accessed from an instance of the Number object, only Number.NaN is allowed.
If a method (such as parseFloat, parseInt) returns NaN that means that the specified parameter cannot be parsed as a number.
Returns negative infinity (any number smaller than MIN_VALUE). Same as the -Infinity global property, see this for details and examples.
The NEGATIVE_INFINITY property is static, it cannot be accessed from an instance of the Number object, only Number.NEGATIVE_INFINITY is allowed.
Returns positive infinity (any number greater than MAX_VALUE). Same as the Infinity global property, see this for details and examples
The POSITIVE_INFINITY property is static, it cannot be accessed from an instance of the Number object, only Number.POSITIVE_INFINITY is allowed.
Returns a reference to the Number.prototype object.
The Number.prototype object allows adding properties and methods to the Number object
that can be used with instances of the Number object, like any predefined property or method.
The prototype property is static, it cannot be accessed from an instance of the Number object,
only Number.prototype is allowed.
Returns a string that represents the current number in exponential notation.
The string contains one digit before the decimal point and a limited number of digits to the right of the decimal point. At the end of the string a signed integer is displayed after the 'e' letter. This signed integer specifies an exponent of ten indicating the multiplier.
fractionDigits
Optional. An integer that specifies the maximum number of digits that can be displayed after the decimal point (from 0 to 20). If not specified, the returned string contains as many digits as can be stored by a floating point number.
Returns a string that represents the current number in a localized form.
For example, it may change the decimal point to a comma, because in some languages the numbers are displayed in that form.
Adding a new method to the Number object and calling it for a primitive numeric value:
Number.prototype.IncreaseByTen = function () {
return (this + 10);
}
var num = 20;
// num is a primitive numeric value, not an instance of the Number object,
// but it will be converted to a Number object before the IncreaseByTen method is called
document.write (num.IncreaseByTen ()); // output: 30
document.write ("<br />");
// representing the same with an instance of the Number object
var num = newNumber (20);
// num is an instance of the Number object
// no conversion is needed
document.write (num.IncreaseByTen ()); // output: 30