3D Game Programming

(C. Jardin) #1
// false

the_opposite_of_the_opposite;
// true

We won’t use Booleans directly like this very often. We’ll usually see compar-
ison operators that make Booleans:

// The > symbol checks for values greater than others
varis_ten_greater_than_six = 10 > 6;
is_ten_greater_than_six;
// true

// Two equal signs check if values are equal
varis_twelve_the_same_as_eleven = 12 == 11;
is_twelve_the_same_as_eleven;
// false

Double Equal Sign vs. Single Equal Sign
A double equal sign (==) in JavaScript checks if something is equal
to something else. It makes no changes to anything—it only checks
values and produces a Boolean.

As we have seen throughout the book, a single equal sign (=) makes
a value equal to something else. Often called the assignment oper-
ator, a single equal sign does change a value—it updates a variable
or assigns it for the first time.

You might be wondering if it’s wise to have two very different oper-
ators look so similar. It isn’t. It’s a very common source of mistakes
—even for people who’ve been programming for years. But, since
it has been around for so long, it probably won’t be changing any
time soon. So be on the lookout for these kinds of mistakes.

We’ll see these kinds of Booleans a lot. In fact, they are in the very next section.


7.4 Repeating and Skipping Code with while and if


Normally JavaScript code is run from top to bottom. The lines of code at the
top of a program are run first. Once the computer is done running those lines,
it moves on to the next lines. This happens all the way to the bottom of a
program file.

But sometimes we don’t want all of the code to run. And other times we want
to run code more than once. For these times, we use control keywords. The
keywords that we’ll see the most in this book are while and if.

Chapter 7. A Closer Look at JavaScript Fundamentals • 74


Prepared exclusively for Michael Powell report erratum • discuss

Free download pdf