Control Flow in JavaScript: If, Else, and Switch Explained

What control flow means in programming
Every day, you make decisions based on conditions, without thinking twice about it: if it’s raining, take an umbrella. If the light is red, stop; otherwise, go. If today is Monday, dread it a little; if it’s Friday, celebrate.
Control flow is the same idea, applied to code — the mechanism that lets a program make decisions and run different instructions depending on what’s actually true at that moment, instead of always executing the exact same steps in the exact same order.

The if Statement
The simplest form of control flow: run a block of code only if a condition is true.
const age = 20;
if (age >= 18) {
console.log("You can vote.");
}Here, age >= 18 is the condition. If it evaluates to true, the code inside the curly braces runs. If it's false, that block is simply skipped entirely — nothing inside it runs at all.

The if-else Statement
Often, you don’t just want to run code when a condition is true — you also want to run different code when it’s false. That’s exactly what else adds:
const age = 15;
if (age >= 18) {
console.log("You can vote.");
} else {
console.log("You cannot vote yet.");
}Now, exactly one of the two blocks runs — never both, and never neither. If the condition is true, the first block runs; if it’s false, the else block runs instead.

The else if Ladder
Real decisions often have more than two possible outcomes. An else if ladder lets you check multiple conditions in sequence, one after another:
const marks = 72;
if (marks >= 90) {
console.log("Grade: A");
} else if (marks >= 75) {
console.log("Grade: B");
} else if (marks >= 60) {
console.log("Grade: C");
} else {
console.log("Grade: F");
}How this runs, step by step
- JavaScript checks
marks >= 90first —72 >= 90isfalse, so it moves on - It checks
marks >= 75next —72 >= 75is alsofalse, so it moves on again - It checks
marks >= 60—72 >= 60istrue, so"Grade: C"prints, and the ladder stops immediately — none of the remaining conditions are even checked

This is an important detail worth internalizing early: the moment one condition matches, the entire rest of the ladder is skipped — order matters, and conditions should generally go from most specific to least specific.
The switch Statement
When you’re checking one single value against many possible exact matches, a switch statement often reads more clearly than a long else if ladder:
const day = "Wed";
switch (day) {
case "Mon":
console.log("Start of the work week");
break;
case "Fri":
console.log("Almost the weekend!");
break;
case "Sat":
case "Sun":
console.log("Weekend!");
break;
default:
console.log("Midweek grind");
}Explaining break clearly
Without break, execution doesn't stop after a matching case — it falls through and keeps running the following cases too, whether or not they actually match:
switch (day) {
case "Mon":
console.log("Start of the work week"); // no break!
case "Fri":
console.log("Almost the weekend!");
break;
}
// If day is "Mon", this logs BOTH lines — an easy, common mistakebreak tells JavaScript "stop here, don't check or run anything further in this switch" — which is exactly why nearly every case needs one, unless you're deliberately using fall-through (as the combined "Sat"/"Sun" case does above, intentionally sharing one block of code between two matching cases).

When to Use Switch vs If-Else
Use if-else (or an else-if ladder) when:
- Checking ranges or comparisons (
marks >= 90,age < 18) —switchonly checks exact equality, not ranges - Checking multiple different variables across conditions, not just one single value repeatedly
- The logic involves complex combined conditions (
age >= 18 && hasLicense)
Use switch when:
- Checking one single value against several possible exact matches (a day of the week, a status code, a specific string)
- You want each specific case clearly, visually separated, which can read more cleanly than a long chain of
else ifchecks against the same variable

Neither is strictly “better” — they’re suited to genuinely different shapes of decision. A grading system based on numeric ranges reads more naturally with if-else; a day-of-week lookup reads more naturally with switch.
Practice Assignments
1. Check if a number is positive, negative, or zero
const num = -7;
if (num > 0) {
console.log("Positive");
} else if (num < 0) {
console.log("Negative");
} else {
console.log("Zero");
}
// "Negative"Why if-else here: this involves comparisons (> 0, < 0), not exact-value matching — exactly the kind of decision if-else handles naturally, and switch can't express directly.
2. Print the day of the week using switch
const dayNumber = 3;
switch (dayNumber) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
case 3:
console.log("Wednesday");
break;
case 4:
console.log("Thursday");
break;
case 5:
console.log("Friday");
break;
case 6:
console.log("Saturday");
break;
case 7:
console.log("Sunday");
break;
default:
console.log("Invalid day number");
}
// "Wednesday"Why switch here: this checks one single value (dayNumber) against several specific, exact matches — precisely the situation switch is built for, and each case reads clearly on its own line.
Try changing num and dayNumber to different values, and predict the output before running each snippet — that habit builds real confidence with control flow far faster than reading alone.
Final Takeaway
Control flow is simply how code makes the same kind of decisions you make constantly in everyday life — if runs something only when a condition holds; else gives you a fallback for when it doesn't; an else if ladder handles several possible outcomes in sequence, stopping at the first match; and switch cleanly handles checking one value against many exact possibilities, as long as you remember break to avoid unintentional fall-through. Neither if-else nor switch is universally better — the right choice depends on whether you're comparing ranges and conditions, or matching one value against a specific, known set of exact possibilities.
Frequently Asked Questions
Can I use switch to check ranges, like age >= 18?
> Not directly — switch compares for exact equality between the value and each case. For ranges or comparisons, if-else (or an else-if ladder) is the right tool instead.
What happens if no case matches in a switch statement and there’s no default?
> Nothing runs — execution simply exits the switch block without executing any code inside it, the same way an if statement with no matching condition and no else simply does nothing.
Is it always necessary to add break after every case?
> Not always — deliberately omitting it (fall-through) is sometimes used intentionally, like the shared "Sat"/"Sun" example above. But omitting it accidentally is a very common bug, so including break by default, and removing it only when fall-through is genuinely intended, is the safer habit.
Does the order of conditions in an else-if ladder matter?
> Yes, significantly — since the ladder stops at the first true condition, conditions should generally be ordered from most specific to least specific (checking marks >= 90 before marks >= 75), or a broader condition could match first and incorrectly skip a more specific one.
Originally published by Mr Madhukar
Read the complete article on Medium with full formatting & reader responses.