Learn how to make decisions and repeat actions in JavaScript with if/else, switch, ternary operators, and loops.
Control Flow
Programs need to make decisions and repeat actions. Control flow statements let you do both.
if / else if / else
The most basic decision structure:
Result:
Rules:
- The condition inside
()is converted to a boolean. - Only the first matching branch runs.
else ifandelseare optional.- Always use curly braces
{}, even for single-line bodies - it prevents bugs when you add lines later.
Combining conditions
Use logical operators to combine conditions:
Result:
Negation
Result:
Ternary operator
A one-line if/else:
Result:
The syntax is condition ? valueIfTrue : valueIfFalse. Use it for simple assignments. Do not nest ternaries - they
become unreadable:
switch
When comparing a single value against many options:
Result:
Important: Always include break. Without it, execution "falls through" to the next case:
Result:
The fall-through is intentional in the Monday–Friday example above (grouping cases), but it is a common bug when forgotten.
for loop
Repeats code a specific number of times:
Result:
The three parts:
- Initialization:
let i = 0- runs once before the loop starts - Condition:
i < 5- checked before each iteration; loop stops whenfalse - Update:
i++- runs after each iteration
Counting backwards
Result:
Stepping by more than one
Result:
while loop
Repeats while a condition is true:
Result:
Use while when you do not know in advance how many iterations you need:
Result:
Infinite loops
A while (true) loop runs forever unless you break out of it:
Result:
Be careful - if you forget the break, the program hangs.
do...while loop
Like while, but the body runs at least once before the condition is checked:
Result:
The body ran once even though the condition was immediately satisfied.
for...of - iterating over values
Iterates over values in an iterable (arrays, strings, maps, sets):
Result:
Works on strings too:
Result:
for...in - iterating over keys
Iterates over keys (property names) of an object:
Result:
Do not use for...in on arrays. It iterates over indices as strings and includes inherited properties. Use
for...of for arrays.
break and continue
break - exit the loop early
Result:
continue - skip to the next iteration
Result:
Nested loops
Loops inside loops:
Result:
Labeled loops
Labels let break and continue target an outer loop:
Result:
Labels are rarely used, but they are good to know about.
Common patterns
Summing values
Result:
Finding a value
Result:
Building a string
Result:
FizzBuzz
The classic beginner exercise:
Result:
Summary
if/else if/elsefor branching decisions.- Ternary
? :for simple one-line conditionals. switchfor comparing a value against many options (rememberbreak).forloop when you know the number of iterations.whileloop when the number of iterations is unknown.do...whilewhen the body must run at least once.for...offor iterating over array values and strings.for...infor iterating over object keys (not arrays).breakexits a loop early;continueskips to the next iteration.
Next up: Functions - reusable blocks of code.