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

JavaScript Control Flow
Programming is not just about writing instructions — it is also about making decisions.
In real life, we make decisions every day.
Example:
If it rains → take an umbrella
If you pass the exam → celebrate
If it is Sunday → relax
Programming works in the same way.
This concept is called Control Flow.
What is Control Flow?
Control flow defines how a program decides which code should run and when.
It allows programs to:
make decisions
execute different code blocks
control the order of execution
JavaScript provides several ways to control program flow:
ifstatementif-elsestatementelse ifladderswitchstatement
1. if Statement
The if statement runs a block of code only when a condition is true.
Syntax:
if(condition){
// code runs if condition is true
}
Example:
let age = 20;
if(age >= 18){
console.log("You can vote");
}
//You can vote
Explanation:
Step 1 → Check condition age >= 18
Step 2 → If true, run the code inside {}
Real Life Example
If a student scores above 40 marks, they pass.
let marks = 50;
if(marks >= 40){
console.log("You passed the exam");
}
// You passed the exam
2. if-else Statement
Sometimes we need two possible outcomes.
Example: Pass or Fail.
Syntax:
if(condition){
// runs if condition is true
}
else{
// runs if condition is false
}
Example:
let marks = 35;
if(marks >= 40){
console.log("Pass");
}else{
console.log("Fail");
}
//Fail
Explanation:
Program checks condition → false → runs else block.
3.Else If Ladder
When there are multiple conditions, we use else if.
Example: grading system.
let marks = 75;
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");
}
//Grade B
The program checks conditions one by one from top to bottom.
As soon as one condition becomes true, the remaining conditions are skipped.
4.Switch Statement
When there are many specific values, switch can be cleaner than multiple if-else.
Syntax:
switch(value){
case option1:
// code
break;
case option2:
// code
break;
default:
// code
}
Example: Day of the week
let day = 3;
switch(day){
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;
default:
console.log("Weekend");
}
//Wednesday
#Why break is Important
The break statement stops the switch from continuing to the next case.
Example without break:
let day = 1;
switch(day){
case 1:
console.log("Monday")
case 2:
console.log("Tuesday")
}
//Output: Monday
// Tuesday
//Because there was no break, the program continued to the next case.
Example with break:
let day = 4;
switch(day){
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");
}
Explanation:
We used switch because we are checking fixed values from 1 to 7.
Final Thoughts
Control flow allows programs to think and make decisions.
Key points:
if→ run code if condition is trueif-else→ choose between two optionselse if→ handle multiple conditionsswitch→ handle many fixed values
Once you understand control flow, you can start building real logic in programs.
Happy Coding 🚀


