distinction is academic, but the knowledge will come in handy later when we discuss
In addition to these six primitive types, there are objects. Unlike primitives, objects
can take on different forms and values, and are more chameleon-like.
Because of their flexibility, objects can be used to construct custom data types. As a
matter of fact, JavaScript provides some built-in object types. The built-in object
types we’ll cover are as follows:
•
Array
•
Date
•
RegExp
•
Map
and
WeakMap
•
Set
and
WeakSet
Lastly, the primitive types number, string, and boolean have corresponding object
types,
Number
,
String
, and
Boolean
. These corresponding objects don’t actually store
a value (that’s what the primitive does), but rather provide functionality that’s related
to the corresponding primitive. We will discuss these object types along with their
primitives.
Numbers
While some numbers (like 3, 5.5, and 1,000,000) can be represented accurately by a
computer, many numbers are necessarily approximations. For example, π cannot be
represented by a computer at all, because its digits are infinite and do not repeat.
Other numbers, such as 1/3, can be represented by special techniques, but because of
their forever repeating decimals (3.33333…), they are normally approximated as well.
JavaScript—along with most other programming languages—approximates real num‐
bers through a format called IEEE-764 double-precision floating-point (which I will
refer to simply as a “double” from here on out). The details of this format are beyond
the scope of this book, but unless you are doing sophisticated numerical analysis, you
probably don’t need to understand them. However, the consequences of the approxi‐
mations required by this format often catch people off guard. For example, if you ask
JavaScript to calculate 0.1 + 0.2, it will return 0.30000000000000004. This does not
mean that JavaScript is “broken” or bad at math: it’s simply an unavoidable conse‐
quence of approximating infinite values in finite memory.
38 | Chapter 3: Literals, Variables, Constants, and Data Types