What is control flow?
Control flow is the order in which the computer runs your instructions.
By default, JavaScript reads code the way you read a book: top to bottom, one line at a time. Control flow statements let you change that order. You can:
- choose which instructions to run (decisions),
- repeat instructions (loops),
- jump out of or skip parts (break, continue, return),
- handle problems without crashing (error handling).
Think of it as the traffic system of your program: signals, turns, and U-turns that decide where the code goes next.
Why do we need control flow?
- To make decisions: Real programs react differently to different situations. A login page lets you in only if the password is correct.
- To avoid doing unnecessary work: Skip steps that don't apply, and stop as soon as the job is done.
- To repeat tasks: Process every item in a list without writing the same instruction again and again.
- To handle mistakes gracefully: Show a friendly message instead of letting the whole app break.
- To make programs smart and interactive: Without control flow, a program would do the exact same thing every time, no matter what.
1. Sequential flow (the default)
Instructions run one after another, in order.
Example: Making tea: boil water → add tea leaves → add milk → add sugar → pour into a cup. Each step happens after the previous one.
2. Conditional statements (making decisions)
if
Run something only if a condition is true.
Example: If it is raining, take an umbrella. If it isn't, nothing special happens.
if...else
Choose one of two paths.
Example: If you have a ticket, enter the cinema hall; else, go to the ticket counter.
if...else if...else
Choose one path out of many, checked in order.
Example: Grading marks: if 90 or above, grade A; else if 75 or above, grade B; else if 50 or above, grade C; else, fail. The first matching condition wins, and the rest are ignored.
switch
Pick one option by matching a value against a list of fixed choices.
Example: A lift panel. You press floor 3, and the lift goes straight to floor 3. Each button (case) leads to one specific action, and there's a default action if the button doesn't match any floor.
Note: In a
switch, each case needs abreak. Without it, the program "falls through" and also runs the next cases, like a lift that doesn't stop at your floor and keeps going.
Ternary operator (? :)
A short, one-line if...else, used for simple either-or choices.
Example: "Is the age 18 or more? Then adult, otherwise minor." One question, two possible answers.
Truthy and falsy values
Conditions don't always need a strict true or false. JavaScript treats some values as false-like (falsy): false, 0, an empty string "", null, undefined, and NaN. Everything else is true-like (truthy).
Example: "If the username box has something typed in it, continue." An empty box counts as falsy, so the program stops there.
Logical operators in decisions
- AND (
&&): all conditions must be true. You can drive if you have a licence AND the car has fuel. - OR (
||): at least one must be true. You can pay by cash OR card. - NOT (
!): flips the result. If NOT logged in, show the login page.
3. Loops (repeating)
Loops repeat a task while a condition holds.
| Loop | When to use | Real-life example |
|---|---|---|
for | Known number of repeats | Calling 40 roll numbers |
while | Repeat while a condition is true | Eating while hungry |
do...while | Run once, then check | Tasting before ordering more |
for...of | Go through each value | Picking fruits from a basket |
for...in | Go through each key/label | Reading form fields |
4. Jump statements (changing direction)
break
Stop immediately and leave the loop or switch.
Example: Searching drawers for your keys. Once you find them, you stop searching.
continue
Skip the current round and move on to the next.
Example: Sorting mail. You skip junk letters and continue with the next one.
return
Exit a function and optionally send back a result.
Example: A cashier finishes billing and hands you the receipt. The job is done, and the result goes back to you.
5. Error handling (dealing with problems)
try...catch
Try something risky. If it fails, catch the problem and handle it calmly instead of crashing.
Example: You try to pay online. If the payment fails, the app shows "Payment failed, please try again" instead of freezing.
finally
Runs no matter what, whether things succeed or fail.
Example: After a cooking attempt, successful or burnt, you always turn off the gas.
throw
Raise your own error on purpose when something is wrong.
Example: A bank teller refuses a withdrawal because the amount is more than your balance and clearly says why.
Quick summary
| Category | Statements | Purpose | Real-life idea |
|---|---|---|---|
| Sequential | (default) | Run line by line | Steps to make tea |
| Decisions | if, else, else if, switch, ? : | Choose a path | Umbrella if raining |
| Loops | for, while, do...while, for...of, for...in | Repeat work | Taking attendance |
| Jumps | break, continue, return | Change direction | Stop when keys are found |
| Error handling | try, catch, finally, throw | Handle problems safely | Payment failed message |
In one line: control flow is how a program decides what to do, when to do it, how many times to do it, and what to do when things go wrong.