Blocks
The following keywords control the flow of execution in a program:
if
, for
, while
, do
, switch
The code block which follows these keywords
(sometimes with a set of instructions enclosed in parentheses in between)
must be surrounded with {
and }
.
For example, this if
statement is bad:
if (x > y)
++x;
… and this if
statement is much better:
if (x > y) {
++x;
}
Switch statements
The block of the switch statement typically contains several case
s and one default
,
each of which should contain a group of statements to execute.
Each of these groups of statements must either break
, throw
, or return
in order to avoid unintentionally falling through to the next group of statements.
switch (myVar) {
case 2:
console.log('Exiting function because myVar is two');
return;
case 1:
console.log('myVar is one');
break;
case 0:
throw 'myVar has an illegal value';
default:
console.log('Nothing special about myVar');
}
The following example illustrates what not to do in a switch statement:
switch (myVar) {
case 2:
console.log('myVar is two');
case 1:
console.log('myVar is one');
}
When myVar
is 1
, it works as expected.
However, when myVar
is 2
, the output is:
myVar is two
myVar is one
What happened here was the case 2
was entered correctly,
but after executing its only statement,
there was no break
statement,
and therefore, the execution fell through to the next case.
Avoid this!
Intentional fall through
Sometimes you want multiple switches to execute the same set of statements. In this case, intentional fall through is allowed, albeit with one condition: That in this case, consecutive labels are used, meaning that there are no statements in between consecutive labels.
switch (myVar) {
case 2:
case 3:
console.log('myVar is two or three');
case 1:
console.log('myVar is one');
}
Note how between case 2:
and case 3:
above,
there was only white space - not executable statements.