JavaScript Numbers
JavaScript numbers can be written with, or without decimals:
Example
var x = 34.00; // A number with decimals
var y = 34; // A number without decimals
Extra large or extra small numbers can be written with scientific (exponent) notation:
Example
var x = 123e5; // 12300000
var y = 123e-5; // 0.00123
JavaScript Numbers are Always 64-bit Floating Point
Unlike many other programming languages, JavaScript does not define different types of numbers, like integers, short, long, floating-point etc.
JavaScript numbers are always stored as double precision floating point numbers, following the international IEEE 754 standard.
This format stores numbers in 64 bits, where the number (the fraction) is stored in bits 0 to 51, the exponent in bits 52 to 62, and the sign in bit 63:
Value (aka Fraction/Mantissa) | Exponent | Sign |
---|---|---|
52 bits (0 - 51) | 11 bits (52 - 62) | 1 bit (63) |
Precision
Integers (numbers without a period or exponent notation) are considered accurate up to 15 digits.
Example
var x = 999999999999999; // x will be 999999999999999
var y = 9999999999999999; // y will be 10000000000000000
Try it Yourself
The maximum number of decimals is 17, but floating point arithmetic is not always 100% accurate:
To solve the problem above, it helps to multiply and divide:
Hexadecimal
JavaScript interprets numeric constants as hexadecimal if they are preceded by 0x.
Never write a number with a leading zero (like 07). Some JavaScript versions interpret numbers as octal if they are written with a leading zero.
By default, Javascript displays numbers as base 10 decimals.
But you can use the toString() method to output numbers as base 16 (hex), base 8 (octal), or base 2 (binary).
Example
var myNumber = 128;
myNumber.toString(16); // returns 80
myNumber.toString(8); // returns 200
myNumber.toString(2); // returns 10000000
Try it Yourself
Infinity
Infinity (or -Infinity) is the value JavaScript will return if you calculate a number outside the largest possible number.
Example
var
myNumber = 2;
while (myNumber != Infinity) {
// Execute until Infinity
myNumber = myNumber * myNumber;
}
Try it Yourself
Division by 0 (zero) also generates Infinity:
Infinity is a number: typeOf Infinity returns number.
NaN - Not a Number
NaN is a JavaScript reserved word indicating that a value is not a number.
Trying to do arithmetic with a non-numeric string will result in NaN (Not a Number):
However, if the string contains a numeric value , the result will be a number:
You can use the global JavaScript function isNaN() to find out if a value is a number.
Watch out for NaN. If you use NaN in a mathematical operation, the result will also be NaN:
Or the result might be a concatenation:
NaN is a number, and typeof NaN returns number:
Numbers Can be Objects
Normally JavaScript numbers are primitive values created from literals: var x = 123
But numbers can also be defined as objects with the keyword new: var y = new Number(123)
Example
var x = 123;
var y = new Number(123);
//
typeof x returns number
//
typeof y returns object
Try it Yourself
Do not create Number objects. It slows down execution speed. The new keyword complicates the code. This can produce some unexpected results:
When using the == equality operator, equal numbers looks equal:
Example
var x = 500;
var y = new Number(500);
// (x == y) is true because x and y have equal values
Try it Yourself
When using the === equality operator, equal numbers are not equal, because the === operator expects equality in both type and value.
Example
var x = 500;
var y = new Number(500);
// (x === y) is false because x and y have different types
Try it Yourself
Or even worse. Objects cannot be compared:
Example
var x = new Number(500);
var y = new Number(500);
// (x == y) is false because objects cannot be compared
Try it Yourself
JavaScript objects cannot be compared.