Number
Summary
An object that represents a number of any kind. All JavaScript numbers are 64-bit floating-point numbers.
Syntax
new Number( value )
- value
- Required. The numeric value.
Examples
// Returns an Object
var thousand = new Number(1000);
console.log(thousand.valueOf() === 1000);
// remember, objects are not primitives
console.log(thousand !== 1000);
// non-strict comparison calls valueOf() implicitly
console.log(thousand == 1000);
// Returns an Object
var googol = new Number(1e+100);
console.log(googol.valueOf() === 1e+100);
// Alfred B. Taylor octal notation for the decimal number 10
var untydu = new Number(012);
console.log(untydu.valueOf() === 10);
// Converting decimal variables to hexadecimal notation
// by http://stackoverflow.com/users/444910/mystifeid
function decimalToHex(d) {
// converting negative numbers to positive using Math.abs()
var hex = Number(Math.abs(d)).toString(16);
hex = "000000".substr(0, 6 - hex.length) + hex;
return hex;
}
console.log(decimalToHex(127) === "00007f");
console.log(parseInt("00007f", 16) === 127);
// Infinity JavaScript Number constant
var andBeyond = 3 / 0;
console.log(andBeyond === Infinity);
Remarks
JavaScript creates Number objects when a variable is set to a number value, for example var num = 255.336;
. It is seldom necessary to create Number objects explicitly.
The Number object has its own properties and methods, in addition to the properties and methods inherited from Object. Numbers are converted into strings under certain circumstances, for example when a number is added or concatenated with a string, as well as by means of the toString method. For more information, see Addition Operator (+).
JavaScript has several number constants. For a complete list, see Number Constants.
For a value that can not be converted to a number, the Number()
function returns the special value NaN (Not-a-Number) which indicates that the expression could not be evaluated to a number.
Only basic types such as strings and boolean values can be converted to numbers. Note, however, that a string can be converted to a number only if it is a numeric string:
Number("123"); // 123
Number("foo"); // NaN
Number("123foo"); // NaN
Integer range
Biggest int possible is 9007199254740992.
Smallest int possible is -9007199254740992.
var biggestInt = Math.pow(2, 53); // 9007199254740992
console.log(biggestInt + 1 === 9007199254740992) ;
console.log(biggestInt + 2 === 9007199254740994) ;
Octals and Hexadecimals
Octal (base-8) and hexadecimal (base-16) numbers can be used in JavaScript.
Octal numbers must begin with 0 (zero) followed by one or more octal digits.
Hexadecimal numbers must begin with 0x.
Properties
The following table lists the properties of the Number object.
Property | Description |
---|---|
constants | Lists the constants of the Number object. |
constructor | Specifies the function that creates an object. |
prototype | Returns a reference to the prototype for a class of number. |
Methods
The following table lists the methods of the Number object.
Method | Description |
---|---|
toExponential | Returns a string that contains a number represented in exponential notation. |
toFixed | Returns a string that represents a number in fixed-point notation. |
toPrecision | Returns a string that contains a number that is represented in either exponential or fixed-point notation and that has a specified number of digits. |
toString | Returns a string representation of an object. |
valueOf | Returns the primitive value of the specified object. |
See also
Related articles
Javascript
Other articles
External resources
- http://en.wikipedia.org/wiki/Octal
- http://en.wikipedia.org/wiki/Hexadecimal
- http://www.2ality.com/2012/04/number-encoding.html
Specification
ECMAScript® Language Specification Standard ECMA-262 5.1 Edition / June 2011
Attributions
Microsoft Developer Network: Article