Numbers can be written with, or without, decimals.
Extra large or extra small numbers can be written with scientific (exponent) notation:
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)|
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:
Never write a number with a leading zero (like 07).
But you can use the toString() method to output numbers as base 16 (hex), base 8 (octal), or base 2 (binary).
Division by 0 (zero) also generates Infinity:
Infinity is a number: typeOf Infinity returns 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.
NaN is a number: typeOf NaN returns number.
But numbers can also be defined as objects with the keyword new: var y = new Number(123)
Don't create Number objects.
They slow down execution speed, and produce nasty side effects:
When using the == equality operator, equal numbers looks equal:
When using the === equality operator, equal numbers are not equal, because the === operator expects equality in both type and value.
Or even worse. Objects cannot be compared:
Primitive values (like 3.14 or 2014), cannot have properties and methods (because they are not objects).
|NEGATIVE_INFINITY||Represents negative infinity (returned on overflow)|
|NaN||Represents a "Not-a-Number" value|
|POSITIVE_INFINITY||Represents infinity (returned on overflow)|
These properties can only be accessed as Number.MAX_VALUE.
Using myNumber.MAX_VALUE, where myNumber is a variable, expression, or value, will return undefined:
Number methods are covered in the next chapter