Chapter 4 described expressions as JavaScript
phrases. By that analogy, statements are JavaScript
sentences or commands. Just as English sentences are terminated and
separated from each other with
periods, JavaScript statements are terminated with semicolons (Optional Semicolons). Expressions are
evaluated to produce a value, but statements are
executed to make something happen.
One way to “make something happen” is to evaluate an expression
that has side effects. Expressions with side effects, such as
assignments and function invocations, can stand alone as statements, and
when used this way they are known as expression
statements. A similar category of statements are the
declaration statements that declare new variables
and define new functions.
JavaScript programs are nothing more than a sequence of statements
to execute. By default, the JavaScript interpreter executes these
statements one after another in the order they are written. Another way
to “make something happen” is to alter this default order of execution,
and JavaScript has a number of statements or control
structures that do just this:
-
Conditionals are statements like
if
andswitch
that make the JavaScript
interpreter execute or skip other statements depending on the value
of an expression. -
Loops are statements like
while
andfor
that execute other statements
repetitively. -
Jumps are statements like
break
,return
, andthrow
that cause the interpreter to jump
to another part of the program.
The sections that follow describe the various statements in
JavaScript and explain their syntax. Table 5-1, at the end of the chapter, summarizes
the syntax. A JavaScript program is simply a sequence of statements,
separated from one another with semicolons, so once you are familiar
with the statements of JavaScript, you can begin writing JavaScript
programs.