}
console.log(
`\tending funs:
${
funds
}
`
);
Control Flow Statements in JavaScript
Now that we’ve got a firm grasp on what control flow statements actually do, and
some exposure to the most basic ones, we can get down to the details of JavaScript
control flow statements.
We’re also going to leave flowcharts behind. They are a great visualization tool (espe‐
cially for those who are visual learners), but they would get very unwieldy past this
point.
Broadly speaking, control flow can be broken into two subcategories: conditional (or
branching) control flow and loop control flow. Conditional control flow (
if
and
if...else
, which we’ve seen, and
switch
, which we’ll see shortly) represent a fork in
the road: there are two or more paths to take, and we take one, but we don’t double
back. Loop control flow (
while
,
do...while
, and
for
loops) repeat their bodies until
a condition is met.
Control Flow Exceptions
There are four statements that can alter the normal processing of flow control. You
can think of these as control flow “trump cards”:
break
Breaks out of loop early.
continue
Skip to the next step in the loop.
return
Exits the current function (regardless of control flow). See
throw
Indicates an exception that must be caught by an exception handler (even if it’s
outside of the current control flow statement). See
The use of the statements will become clear as we go along; the important thing to
understand now is that these four statements can override the behavior of the control
flow constructs we’ll be discussing.
Broadly speaking, control flow can be broken into two subcategories: conditional con‐
trol flow and loop control flow.
68 | Chapter 4: Control Flow