Mike: Why are those types called primitive?
Professor: Simply because they only carry a single value and nothing else. This is in
contrast to objects, which are much more complex, as you will see.
Let me ask you something befor e we continue. What will be the values of x and y
after the following code executes?
var x;
var y;
y = 10;
x = y + 5;
y = 20;
Mike: 25 and 20?
Professor: Think again.
Mike: Yo u said that primitive values o nly held a sing le value and that the last value
always overrode the previously assigned values. So y should be 20.
Professor: That’s true. y has the value 20. But do n ot overlook the fact that at the
time of computing the value of x in the second to last line of the code, the value of y
is 10. Therefore, the value of x is 15 and not 25. Note that, just as in the rea l world
you cannot change what happened in the p ast, the last line of the code has no way of
affecting the value of x, which was determined in the previous line.
Let’s return to types. The first of th e types is number, which represents all kinds of
numbers, integers as well as real numbers. In computer terminology w e often call
real numbers floating-point numbers beca use of the format in which th ey are stored
in memory. If you are interested, you can check the IEEE 754 standard for details
of how this format is defined. JavaScript uses the 64-bit flo ating-point format for
both real and integer numbers. That means it can represent real numbers as huge as
±1.7976931348623157 × 10
308
and as tiny as ±5 × 10
−324
. I think those bounds are
not something you should worry about right now, I just give them to you as a curiosity.
However, the bounds can become problematic when it co mes to integers, wh ic h are
bounded to the values between -9007199254740992 and 9007199254740992. If yo u
study the IEEE floating-point standard, you might discover that these weird numbers
are in fact (plus a nd minus) two raised to the power of 53.
Mike: Where do these limitation s come from?
Professor: You know, numbers have limited space in memory, like a mileage counter
in a car. A mileage c ounter usually has six digits, so it can count miles up to 9 99,999.
Something similar happens w ith numbers stored in computer memory. However, they
are stored as binary numbers, and thus the highest and lowest values seem unusual
when expressed in decimal form.
What is probably much more important for you right now is that numbers ca n be
represented by numeric litera ls in forms that y ou can find in the above table. The first
6.4. Values and Types 111