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
Integers (numbers without a period or exponent notation) are considered accurate up to 15 digits.
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:
You can use the toString()
method to output numbers as base 16 (hex), base 8 (octal), or base 2 (binary).
Infinity (or -Infinity) is the value JavaScript will return if you calculate a number outside the largest possible number.
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 number is not a legal number.
You can use the global JavaScript function isNaN()
to find out if a value is 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:
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: