Control Flow in JavaScript: If, Else, and Switch Explained
Control Flow in JavaScript (Beginner Friendly Guide)
When writing programs, code normally runs line by line from top to bottom. But in real applications we often need the program to make decisions. That is where control flow comes in.
Control flow describes the order in which instructions in a program are executed and how that order can change based on conditions. (web.dev)
In this article we’ll learn:
What control flow means
if statement
if-else statement
else if ladder
switch statement
When to use switch vs if-else
1. Understanding Control Flow With Real Life Examples
Programming decisions work exactly like real-life decisions.
Example:
If it is raining → take an umbrella
If marks are above 90 → grade A
If today is Sunday → relax
Programs do the same thing:
Condition → decision → action
Without control flow, programs would simply run every line of code sequentially without making decisions. (web.dev)
2. The if Statement
The if statement runs code only when a condition is true. (GeeksforGeeks)
Example: Checking age
let age = 20;
if (age >= 18) {
console.log("You can vote");
}
Step-by-step execution
Program checks age >= 18
If condition is true
Code inside {} runs
Output:
You can vote
If the condition is false, nothing happens.
3. The if-else Statement
Sometimes we want two possible outcomes.
Example: pass or fail.
let marks = 40;
if (marks >= 50) {
console.log("You passed");
} else {
console.log("You failed");
}
How code runs
Check condition → marks >= 50
If true → "You passed"
If false → "You failed"
Output:
You failed
4. The else if Ladder
What if there are multiple possibilities?
Example: grading system.
let marks = 85;
if (marks >= 90) {
console.log("Grade A");
}
else if (marks >= 70) {
console.log("Grade B");
}
else if (marks >= 50) {
console.log("Grade C");
}
else {
console.log("Fail");
}
Execution flow
Check >= 90 → false
Check >= 70 → true
Stop checking further
Output:
Grade B
Important idea:
The program stops checking conditions once one becomes true.
5. The switch Statement
A switch statement is used when we want to compare one variable with many fixed values. (Wikipedia)
Example: printing day of week.
let day = 2;
switch(day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
case 3:
console.log("Wednesday");
break;
default:
console.log("Invalid day");
}
Output
Tuesday
6. Why break is Important in switch
Inside switch, the break statement stops execution after a case runs.
Without break, the program continues to run the next cases.
Example without break:
switch(day) {
case 1:
console.log("Monday");
case 2:
console.log("Tuesday");
}
Output
Tuesday
But if day was 1, output would be:
Monday
Tuesday
Because the program falls through to the next case.
7. When to Use switch vs if-else
Many beginners misuse these.
Use if-else when conditions are complex
Example:
if (age > 18 && country === "India") {
console.log("Eligible to vote");
}
Here we check logic conditions.
Use switch when checking one variable against multiple values
Example:
switch(color) {
case "red":
case "blue":
case "green":
}
This is cleaner than many else-if checks.
Quick comparison
Situation | Best choice |
Complex conditions | if / else |
Range checks | if / else |
Checking many fixed values | switch |
Multiple equality checks | switch |
Flowchart Idea: If-Else Decision
Condition
|
True / False
/ \
Run code A Run code B
Switch Branching Diagram
variable
|
-------------------
| | |
case1 case2 case3
| | |
code code code
Assignment (Practice)
Try these in your console.
1️⃣ Check positive, negative, or zero
let num = -5;
if (num > 0) {
console.log("Positive");
}
else if (num < 0) {
console.log("Negative");
}
else {
console.log("Zero");
}
Why if-else?
Because we are checking value ranges, not fixed values.
2️⃣ Print day of week using switch
let day = 3;
switch(day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
case 3:
console.log("Wednesday");
break;
default:
console.log("Invalid day");
}
Why switch?
Because we compare one variable with fixed values.
Final Advice
Control flow is one of the most important concepts in programming.
Without it, programs cannot:
make decisions
react to user input
handle different situations
Mastering if, else, and switch will make your JavaScript code logical instead of just sequential.


