Control Flow in Programming (JavaScript)
Understand all the control flow in JavaScript with Examples.

1. Introduction: What is Control Flow in Programming?
Imagine a normal day in your life.
You wake up and think:
If it is morning, I will brush my teeth
If it is Sunday, I will rest
If it is Monday, I will go to work
Your brain is constantly making decisions.
Programming works the same way.
A computer program also needs to decide:
Should this code run?
Or should another code run?
Or should it skip something?
This process is called Control Flow.
Definition
Control Flow determines the order in which instructions are executed in a program.
Without control flow, a program would execute every line from top to bottom with no decision-making.
❊Example Without Control Flow
console.log("Wake up");
console.log("Go to gym");
console.log("Go to office");
console.log("Sleep");
The program will run every line.
But what if today is Sunday?
You might not want to go to the office.
This is where control flow statements come into play.
❊ Types of Control Flow Statements
The most common ones are:
if statement
if-else statement
else-if ladder
switch statement
These help programs make decisions.
2. The if Statement
The if statement executes code only if a condition is true.
Syntax
if (condition) {
// code runs if condition is true
}
❊ Real Life Example
Imagine a school rule:
If a student’s marks are greater than 40, they pass.
let marks = 50;
if (marks > 40) {
console.log("You passed the exam");
}
Step-by-Step Execution
Program reads the value of
marksIt checks the condition
marks > 40If the condition is true, the code inside
{}runsIf the condition is false, the program skips it
Example Output
You passed the exam
When the condition is false
let marks = 30;
if (marks > 40) {
console.log("You passed");
}
Output:
(no output)
Because the condition is false.
3. The if-else Statement
Sometimes we want the program to do something else if the condition is false.
That is where if-else is used.
Syntax
if (condition) {
// runs if condition is true
} else {
// runs if condition is false
}
❊ Real Life Example
At a movie theatre:
If age ≥ 18 → Allowed
Else → Not allowed
Example
let age = 16;
if (age >= 18) {
console.log("You can watch the movie");
} else {
console.log("You are not allowed");
}
❊ Step-by-Step Execution
Program checks
age >= 18If true → first block runs
If false →
elseblock runs
Output
You are not allowed
4. The Else If Ladder
Sometimes there are multiple conditions.
Example: grading system
| Marks | Grade |
|---|---|
| 90+ | A |
| 75+ | B |
| 50+ | C |
| <50 | Fail |
This is where else-if ladder is useful.
Syntax
if (condition1) {
} else if (condition2) {
} else if (condition3) {
} else {
}
Example
let marks = 82;
if (marks >= 90) {
console.log("Grade A");
}
else if (marks >= 75) {
console.log("Grade B");
}
else if (marks >= 50) {
console.log("Grade C");
}
else {
console.log("Fail");
}
⤑ Step-by-Step Execution
Marks = 82
Check
marks >= 90→ falseCheck
marks >= 75→ trueProgram prints Grade B
Remaining conditions are skipped
Output
Grade B
5. The Switch Statement
The switch statement is used when there are many possible fixed values.
It is commonly used for:
menus
days of week
months
options
⤑ Syntax
switch(expression) {
case value1:
code
break;
case value2:
code
break;
default:
code
}
⤑ Example: Day of Week
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");
}
⤑ Step-by-Step Execution
day = 3Switch checks cases
case 3matchesPrints Wednesday
❊ Understanding break in Switch
break stops execution.
Without break, the program continues executing the next cases.
⤑ Example Without break
let day = 2;
switch(day) {
case 1:
console.log("Monday");
case 2:
console.log("Tuesday");
case 3:
console.log("Wednesday");
}
Output
Tuesday
Wednesday
This happens because there is no break.
6. When to Use Switch vs If-Else
⤑ Use if-else when:
- conditions are ranges
Example:
marks > 90
age >= 18
temperature > 30
⤑ Use switch when:
- checking specific values
Example:
day = Monday
role = admin
menu option = 1
⤑ Example
Good for if-else
if (marks > 90)
Good for switch
switch(day)
❊ Comparison Table
| Feature | if-else | switch |
|---|---|---|
| Range conditions | Yes | No |
| Exact values | Yes | Yes |
| Readability with many cases | Hard | Easier |
| Common use | logic | menus/options |
❊ Conclusion
Control flow is one of the most fundamental concepts in programming.
It allows programs to:
make decisions
run different code paths
handle real-world logic
The most important control flow statements are:
if
if-else
else-if ladder
switch
Understanding when to use each of them will make your programs cleaner and easier to understand.
❊ Want More…?
I write articles on blog.prakashtsx.com and also post development-related content on the following platforms:



