Statements
Staments are something that we execute to "make something happen" (Flanagan).
Statements could be expressions that have side effects (e.g. assigning to a variable, calling a function), or control statements that govern the flow of our program.
We will cover control statements here.
Conditionals
These make a section of your code executed only when a condition is true.
if, else
You can just include the if
clause:
or the if
and else
clauses:
or even stack multiple of them together:
switch, case
Put the expression to test inside
switch (<expression>)
.Specify the conditions to match using the
case <expression>:
labels, followed by statements that your program should run if the label is matched, optionally followed by thebreak
keywordYou may put one
default:
label that will be matched if othercase
labels fail to match.
switch
will find a matching case
label (using strict equality), and run all code below it until it finds a break
statement.
Loops
These make a section of your code executed repeatedly while a condition is true.
while
It checks if the loop condition inside while (...)
is truthy before executing your loop statements.
- If it does, it executes your loop statement (and then goes back into checking the loop condition again)
- If it doesn't, it skips over the entire
while
block
do–while
Similar to while
, but it runs your loop statements first before checking the loop condition inside while (...)
. (Your loop statements are guaranteed to run at least once.)
for
Simplifies loops that follow a common pattern (you can put the initialization, condition, and updates in one line).
for–of
Iterates the content of something (the something must be iterable, e.g. an array).
for–in
Iterates the keys/properties of something.
Note that it iterates all inherited properties, EXCEPT (1) properties that are symbols and (2) properties that are not enumerable.
Jumps
return
Exits from a function (return
), optionally with a value (e.g. return 5
).
break
Exits from the nearest loop block (while, do-while, or for).
continue
Restarts the nearest loop block at the next iteration.
throw
The throw
statement throws an exception (signals an error). You can throw anything (e.g. throw 123
), but normally, you do throw new Error('<errorMessage>')
.
When you throw an exception, JavaScript will exit the current block until it find something that "catches" the exception (see the next section). If nothing catches it, then the exception will be treated as an error, and your program execution will stop there.
try, catch, finally
You use try-catch to handle exceptions that occur in your program (rather then having JavaScript terminate with an error).
- The
try
block marks the section whose exceptions we want to handle - The
catch
block is executed when an exception is throws inside thetry
block - The
finally
block (optional) is always executed after your program exits thetry
orcatch
block
References
- JavaScript: The Definitive Guide, 7th Edition (David Flanagan) — Chapter 5. Statements