ptg16476052
482 LESSON 17: Introducing JavaScript
do long void
double native volatile
else new while
enum null with
eval package yield
export private
extends protected
false public
Not all the reserved words in Table 17.2 are currently used in JavaScript; some have been
placed off limits because they might be added to the language in the future.
Here are a couple of additional notes on variable assignment. You don’t have to assign a
value to a variable when you declare it. You can declare the variable without an assign-
ment so that it can be used later. For example:
var myVariable;
If you entered myVariable in the console as an expression, it would be evaluated to unde-
fined because nothing has been assigned to it. However, if you enter the name of an as
yet undeclared variable (like myNewVariable), you’ll see an error message.
You can also assign new values to variables after they’ve been declared, as follows:
myVariable = "My value";
Control Structures
To get your scripts to actually do something, you’ll need control structures, which come
in two main varieties. There are conditional statements, which are used to make deci-
sions, and loops, which enable you to repeat the same statements more than once.
The if Statement
The main conditional statement you’ll use is the if statement. The statements inside an
if statement are executed only if the condition in the if statement is true. If you were
writing code in English rather than JavaScript, an if statement would read like this: “If
the background of this element is blue, turn it red.” There’s also an else clause associ-
ated with if. The statements in the else clause are executed if the if statement’s condi-
tion is false. An if statement with an else clause reads like this: “If the value of this
variable is blue, change it to red; otherwise, change it to blue.”