Control Flow in JavaScript: If, Else, and Switch Explained
Making Your Code Think and Decide Like You Do
What Control Flow Means in Programming
Control flow is simply the order in which your code executes.
By default, JavaScript runs code top to bottom, line by line. But real-world logic isn’t always that straightforward.
Sometimes you want,
“If this happens, do this”
“Otherwise, do something else”
That decision-making ability is what control flow gives you.
The if Statement
The if statement is the most basic decision-maker.
It runs a block of code only if a condition is true.
let age = 18;
if (age >= 18) {
console.log("You are an adult");
}
If the condition is true, the code runs.
If it's false, JavaScript simply skips it.
The if-else Statement
What if you want to handle both outcomes?
That’s where else comes in.
let age = 16;
if (age >= 18) {
console.log("You can vote");
} else {
console.log("You are underage");
}
if handles the true case
else handles everything else
The else if Ladder
Sometimes you have multiple conditions, not just two.
let score = 75;
if (score >= 90) {
console.log("Grade A");
} else if (score >= 70) {
console.log("Grade B");
} else if (score >= 50) {
console.log("Grade C");
} else {
console.log("Fail");
}
JavaScript checks conditions top to bottom
The first true condition wins, and the rest are ignored
The switch Statement
Think of switch like a menu selector.
You have one value and you want to check it against many exact options—not ranges, not complex logic—just direct matches.
Imagine this:
“Based on this one value, which exact case should run?”
That’s exactly what switch does.
let role = "admin";
switch (role) {
case "admin":
console.log("Full access");
break;
case "user":
console.log("Limited access");
break;
case "guest":
console.log("View only");
break;
default:
console.log("Unknown role");
}
What’s Happening Here?
JavaScript checks:
Isrole === "admin"? --> Yes --> run that blockThen it stops immediately because of
break
If there was no match:
- It would go to
default(likeelse)
Why break is Important
Without break, JavaScript keeps running the next cases (this is called fall-through).
let role = "admin";
switch (role) {
case "admin":
console.log("Admin panel");
case "user":
console.log("User panel");
}
//Output:
//Admin panel
//User panel
That’s usually not what you want.
When It Feels More Natural Than if-else
Instead of writing
if (role === "admin") { ... }
else if (role === "user") { ... }
else if (role === "guest") { ... }
You write it once:
switch (role) { ... }
Cleaner
Easier to scanBetter when values are fixed
When to Use switch vs if-else
Here’s the practical difference:
Use if-else when :
You’re dealing with ranges or conditions
Logic involves comparisons (
>,<,!==, etc.)
Example: checking marks, age, validation rules
Use switch when:
You’re comparing one variable to fixed values
You want cleaner, more readable code
Example: menu options, roles, days of the week
Conclusion
Control flow is what makes your code feel smart instead of static.
if→ simple decisionsif-else→ two outcomeselse if→ multiple conditionsswitch→ clean handling of fixed values