instinctively know that 19 has the value 19 and that 23 has the value 23. Such values
that appear directly in a program as fixed values are called literals. Literal is a lso
any text between (double or single) quotes. For example, "Hello there!" is a text
literal. Beside literals, prog rams also contain various names used to refer to dierent
entities such a s variables that sto re values. Such names are called identifiers. It is
crucial that you understand the dierence between the two.
For example, we can put sum from our last example in quotes:
document.write("sum");
Now sum is no longer an identifier but a text literal. If you reload the page, you’ll see
“sum” instead of “42 in the browser window.
Maria: Have you told us how to write comments in JavaScript?
Professor: Oh, yes. The language supports two styles of comments. A single line
commen t b egin s with a double slash and ends at the end of the line:
//This is only a humble remark.
A multiple line comment can span over several lin es and goes between two pairs of
characters /* and */:
/* This is an elaborate discussion about the meaning of life,
which will be completely ignored by the browser. */
6.4 Values and Types
Professor: You just learned that a computer program is a list of tasks to be carried
out by the computer. Most of the time these tasks involve manipulating value s. For
example, computing the velocity of a bouncing ball in a simulation software or com-
puter game, or calculating interest from the principal investment amount, the annual
interest rate, and th e number of ye ars the money is invested. Yo u also lea rned that a
value can be represented either as a literal, such as a fixed number o r text in quotes, or
stored in a named variable. Regardless of a f orm that a value takes, you shall always
ask yourselves about the type of that value.
JavaScript distinguishes between four die rent primitive data types, w hich are sum-
marized in the following table together with some example values:
Type Example Values
number 42 2.71828 0xf8 6.02 2e23 I nfinity NaN
boolean true false
string "something" "42"
undefine d undefined
110 Meeting 6. Behavior
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
aecting 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
two, (42 and 2.7 1828) don’t need any explanation. The literal 0xf8 may also be
familiar to you.
Maria: Is that a hexadecimal? In CSS we put the hash sign (#) before hexadecimal
numbers.
Professor: That’s right. Except that in JavaScript, you put either 0x or 0X before a
hexadecimal literal.
Mike: Is 6.022e23 the Avogadro constant? It’s familiar f rom school. So m aybe e
means “time s ten raised to the power of”?
Professor: Exactly. That’s the so -called e notation, which is used in place of scientific
(exponen tial) notation if superscripted exponents like 10
23
cannot be displa yed as in
most calculators or co mputer programs. Instead , th e letter e (o r E) is used to mean
“times ten raised to the power of. Therefore, 6.0 22e23 reads as 6.022 × 10
23
.
Maria: Something still doesn’t fit ju st right. If y ou can write real numbers up to the
order of 10
308
, why can’t you write integers quite so large as well?
Professor: That’s a good question. Let’s try the fo llowing example:
var biggest = 9007199254740992; //Take the biggest possible
biggest += 2; //integer and increase it
document.write(biggest); //by two.
In the browser window yo u see the expe cted result.
So wheres the catch? The fact is that it is not the range that is the problem but the
precision. A 64-bit floating point value is guaranteed to be precise up to 15 significant
digits. That is counted from the first digit that is dierent from zer o, no matter how
big or h ow small the value may be. The numbers with a larger number of significant
digits may or may not be exact. For example, if you increm ent the bigge st possible
integer by just one, you get 9007199254740 992, and if you incre ment it by thre e, y ou
get 900719925 4740994, which are both wrong. The corr ect result, wh ic h we get by
adding two, is just a coincidence. You can also try incrementing the value by one
many times a nd you’ll never get anywhere.
Enough of numbers for now. The second of the primitive data types is Boolean. It is
used in lo gical calculus of truth values, and variables of the Boolean type may assume
only the values true and false. Next, the re is a data type called string, which is
nothing more than plain text. In JavaScript, everything you put b etween (single or
double) quotes is considered a string literal.
The last of the primitive types is undefined. This is a specia l type with only a single
value undefined, which is assigned to a variable that h as been declared but not given
112 Meeting 6. Behavior
any value yet. For example, if you run the f ollowing code, you will see undefined
written in the browser window:
var x;
document.write(x); //Writes undefined
Closely related to the undefined value is null. This is a special value that belongs
to the Object type but in practice, nu ll can be seen as an exclusive member of its
own type. It is usually used to indicate the absence of a value, just like undefined is.
There is a slight dierence in the meaning of both, th ough. undefined is considered
as an unexpected, or erroneous, absence of value, and nul l as a normal, or expected,
absence of a value.
Maria: Normal in what sense?
Professor: For example, if a program needs a value to be entered from the user, then
it is n ormal to expect that the variable used to store that value do esn’t have a value
before the user enters it. On the othe r hand, the p rogramm er m ight have forgotten to
initialize a variable that should have been initialized, which results in an unexpected
absence of value.
By the way, null is a lan guage keyword while undefined is a variable whose value
is set to undefined. But since undefined is read-only acco rding to the specification,
that fact sh ould be no more than a technical curiosity to you. And the same g oes for
NaN and Infin ity—they are rea d-only variables too.
Mike: I noticed these two between the numbers in our last table on page 110. What
are they?
Professor: Oh, thanks, I forgot to explain. Infinity is infinity, which means some-
thing without any limit. I don’t want to talk too much about it, for some great minds
have already gone nuts because of infinity. For us, let it be that infinity is, for example,
something divided by z ero:
document.write(1 / 0); //Writes Infinity
In JavaScript, infin ity is in fact anything larger than the largest 64-bit floating point
number. For example:
document.write(1.797693134862316e308); //Writes Infinity
The identifier NaN stands for “Not a Number. NaN a rises if you attempt to carry out
an operation that cannot return a well defin ed numeric value, such as divide zero by
zero or m ultiply a number by undefined (declared but uninitialized variable). For
example:
document.write(0 / 0); //Writes NaN
document.write(3 * undefined); //Writes NaN
6.4. Values and Types 113
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.106.191