Numbers
Floating point numbers
Whether or not you enter a number using a decimal point, JavaScript stores all numbers as floating pointA finite representation of a number, which might be an approximation. numbers. When a number has been rounded off so that not all digits after the decimal point appear, it is clear that the number is not being stored exactly as it was entered. What makes JavaScript tricky is that even if a number is displayed without a decimal point, there is no guarantee that its exact value is being stored. (We'll see an example of this phenomenon soon.)
You might also encounter special numbers such as Infinity
and -Infinity
, for example if you divide by zero. You might encounter the value NaN
(denoting Not a Number), if you make calculations involving Infinity
and -Infinity
, which is not recommended, as the results are not likely to correspond to what is mathematically correct. That is, it is best to think of Infinity
and -Infinity
as ways of finding out that something went wrong rather than as mathematically consistent values that will behave in a predictable manner. Speaking of inconsistency, despite its name, NaN
is of type number
.
Functions using numbers
Common operations on numbers include the following:
Operator | JavaScript |
---|---|
addition | + |
subtraction | - |
multiplication | * |
division | / |
remainder | % |
Glimpse of the future
In a later discussion of the Math object, we will consider exponentiation and other functions.