A page in the |
---|
Introduction |
Basics |
Statements · Control Flow Statements · Comments · Objects · Functions · Style |
Features |
Scoping · Inheritance · DOM |
It has been suggested that this page or section be merged into Wikibooks:JavaScript. (Discuss)
A control flow statement modifies a program's control flow. A control structure additionally contains another statement which is executed under specified conditions, by modification and/or validation of the environment. Furthermore, loops are structures which repeat their statements while the environment is validated by a given test, or "condition." (And a loop which does not modify its environment at some point in a way which ultimately makes its condition false is an infinite loop, a common type of bug.)
Structures[]
if[]
ECMA | Mozilla | Microsoft |
---|---|---|
[1] | [2][3] | [4] |
The if
statement is the most fundamental control structure. It branches according to the truth value of a Boolean expression. If the condition is truthy, the supplied block will be executed. Optionally, a second statement can be provided, immediately following the if
block and starting with the keyword else
, which will be executed if the condition is falsy.
if (condition)
statement
if (condition)
statement
else
statement
switch[]
ECMA | Mozilla | Microsoft |
---|---|---|
[5] | [6][7] | [8] |
The switch
(or switch..case
) statement tests an expression for equality with each of several values. If a matching value is found, the statements immediately after it are executed; if none of the values match and a default
case exists, it is executed. If the matching case's control flow never passes over a break
statement and it is followed by another case, the next case is also executed.
switch(expression){
case value1:
...
break;
case value2:
...
break;
case etc:
...
break;
default:
...
}
This is a good replacement for several compounded if..else
statements which only test the same expression for equality with several values.
Loops[]
while[]
The while
is the simplest of the loops: it repeatedly checks a Boolean condition and then executes a statement until the condition is falsy.
while (condition)
statement
do..while[]
The do..while
loop is exactly the same as the while
loop, with the sole exception that it checks its condition only after having already run its statement once. This is useful for situations that involve performing a calculation or getting some input, then using its result to decide whether or not to repeat the action.
do
statement
while (condition);
for[]
A for
loop is slightly more complex. It comprises four parts: an initialiser, a condition, a step, and a statement.
Execution of a for
loop follows a set order:
- The initialiser is executed. This may contain a variable declaration.
- The condition is checked. If the result of this expression is falsy, the loop will exit and normal control flow will resume.
- The statement is executed.
- The step is executed. This may be any expression; the return value is ignored.
- Go to 2.
If you find yourself writing a while
loop that uses an initialiser and/or step, consider using a for
instead. Any of the parts can be left blank (followed by a semicolon as appropriate), in which case they will be ignored.
for (initialiser; condition; step)
statement
for..in[]
The for..in
loop allows looping over all the keys of an object. Two caveats apply: if you are not careful to check the keys with Object.prototype.hasOwnProperty(), you may get a property on the object's prototype by accident; and for..in
loops are exceedingly slow in current popular JavaScript implementations. The usual for
loop should be your default choice: resort to the for..in
loop only where absolutely necessary. For each element of object, the key shall be assigned to the variable key, and then the statement shall be executed.
for (var key in object)
statement
Jumps[]
break[]
A break
statement exits the specified labelled block statement or the immediately enclosing loop statement.[9]
break;
break label;
continue[]
continue
can only be used inside loops. It skips the rest of the specified loop's current iteration and begins its next. If no label is specified, the statement applies to the immediately enclosing loop.[10]
continue;
continue label;
Labels[]
Any block statement can be labelled by preceding it or its keyword with an identifier followed by a colon. Labels can be supplied to jump statements to specify a target to which to jump, but if one finds one's self using these, it's quite likely that one's code is overly complex, and the design ought to be simplified and refactored — perhaps by moving some of those inner loops into functions.