文章目錄
Undefined (primitive value)
console.log(typeof(undefined)); // "undefined"
Null (primitive value)
console.log(typeof(null)); // "object" (a lie!)
You might think this means null is an object. Don’t fall into this trap! It is a primitive value, and it doesn’t behave in any way like an object.
Unfortunately, typeof(null) is a historical accident that we’ll have to live with forever.
Booleans (primitive value)
there are only two boolean values: true and false.
number (primitive value)
|
|
At first, numbers might seem unremarkable. Let’s look closer!
Limited Precision
There is a popular snippet claiming that numbers in JavaScript are broken:
|
|
However, this behavior is not specific to JavaScript — and it makes sense if we remember that our JavaScript universe might, after all, be a simulation.
這種行為並不是 JavaScript 特有的—— 記得,我們的 JavaScript 世界畢竟是一個模擬出來的世界。
Since computer memory is limited, you cannot store numbers with infinite precision
Floating point math is a clever invention to express a wide range of numbers (including fractions) with a sequence of bits of a particular length (e.g. 64).
That’s the kind of math that powers JavaScript numbers.
It might remind you of digitizing a vinyl record: the input is analog, but the result necessarily “snaps” to the closest values that can be stored:
Both 0.1
and 0.2
get “rounded” to the closest available numbers. Rounding mistakes accumulate, so summing them up doesn’t give us exactly 0.3
.
Floating Decimal Point
Another interesting aspect of the floating point math is that the decimal precision “floats” depending on how large the number is. The closer we are to 0
, the more precision numbers have, and the closer they “sit” to each other:
小數精度"浮動"取決於數字的大小。我們越接近0,數字的精度越高,它們彼此"坐"得越近:
As we move from 0
in either direction, we start losing precision. At some point, even two closest representable numbers stay further apart than by 1
:
|
|
This might seem confusing. However, for most practical calculations, floating point math works pretty well. It balances the tradeoffs of expressing a wide range of numbers, a reasonably high precision, and predictable memory use.
對於大多數實際計算,浮點數學效果很好。它平衡了表達範圍廣泛的數字、相當高的精度和可預測的記憶體使用之間的權衡。
In our JavaScript universe, there is exactly one number value for every mathematical number that can be represented with 64-bit floating point math. 每個可以用 64 位浮點數學表示的數學數字都只有一個數值。
Special Numbers
It is worth noting that floating point math includes a few special numbers. You might occasionally run into NaN
, Infinity
, -Infinity
, and -0
. They exist because sometimes you might execute operations like 1 / 0
, and JavaScript needs to represent their result somehow. The floating point math standard specifies how they work, and what happens when you use them.
Here’s how special numbers may come up in your code:
|
|
Out of these special numbers, NaN
is particularly interesting. NaN
, which is the result of 0 / 0
and some other invalid math, stands for “not a number”.
You might be confused by why it claims to be a number:
|
|
However, there is no trick here. From JavaScript perspective, NaN
is a numeric value. It is not null, undefined, a string, or some other type. But in the floating point math, the name for that term is “not a number”. So it is a numeric value. It happens to be called “not a number” because it represents an invalid result.
recap
- Undefined: Only one value,
undefined
. - Null: Only one value,
null
. - Booleans: Two values:
true
andfalse
. - Numbers: One value for each floating point math number.