Control Flow in JavaScript: If, Else, and Switch Explained
A beginner friendly guide to making decisions in code ⚙️

When we write programs, we are not just asking the computer to execute instructions one after another. Real programs often need to make decisions based on conditions. Sometimes we want something to happen only if a certain requirement is met. Other times we want different actions depending on different situations.
This ability to control the path that a program follows is called control flow.
In simple terms, control flow determines which part of the code runs and when it runs.
To understand this better, think about a small real life example.
Imagine you are leaving your house in the morning.
If it is raining, you take an umbrella.
If it is not raining, you simply leave.
Your decision depends on a condition. Programming works in a very similar way. The program checks a condition and decides which instruction should run.
JavaScript provides several control flow structures that allow us to make these decisions.
In this article we will explore
The if statement
The if else statement
The else if ladder
The switch statement
When to use switch vs if else
Everything will be explained with simple beginner friendly examples.
The if statement
The if statement is the most basic way to control program flow. It allows the program to run a block of code only when a condition is true.
Think of it as asking a question.
If the answer is true, the code runs.
If the answer is false, the code is skipped.
Here is a simple example.
let age = 20;
if (age >= 18) {
console.log("You are eligible to vote.");
}
console.log("Program finished.");
Output
You are eligible to vote.
Program finished.
Step by step explanation
First the variable age is stored as 20.
The program checks the condition age >= 18.
Since 20 is greater than 18, the condition becomes true.
Because it is true, the message inside the if block runs.
If the age was smaller than 18, the message inside the block would simply not run.
The program would continue with the next instruction.
The if else statement
Sometimes we want the program to do one thing if the condition is true and another thing if it is false.
For that we use the if else statement.
It provides two possible paths for the program.
Example
let marks = 45;
if (marks >= 50) {
console.log("You passed the exam.");
} else {
console.log("You did not pass the exam.");
}
Output
You did not pass the exam.
Explanation
The program checks whether marks are greater than or equal to 50.
Since 45 is less than 50, the condition becomes false.
Because of this, the code inside the else block runs.
This structure is very useful when you want to clearly handle both outcomes of a condition.
The else if ladder
In many situations we have more than two possibilities.
For example, when checking grades, we may want different messages depending on the marks obtained.
This is where the else if ladder becomes useful.
It allows the program to check multiple conditions one after another.
Example
let score = 82;
if (score >= 90) {
console.log("Grade A");
} else if (score >= 75) {
console.log("Grade B");
} else if (score >= 60) {
console.log("Grade C");
} else {
console.log("Grade D");
}
Output
Grade B
How the program runs
The program first checks if score is greater than or equal to 90.
Since 82 is not greater than 90, it moves to the next condition.
Next it checks whether score is greater than or equal to 75.
This time the condition is true.
Because this condition is true, the program prints Grade B and stops checking the remaining conditions.
Only the first true condition gets executed.
This step by step checking is what makes the else if ladder powerful for handling multiple scenarios.
The switch statement
Another way to control program flow is the switch statement.
Switch is commonly used when we want to compare a variable with several fixed values.
It can sometimes make the code cleaner than writing many else if statements.
Example
let 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;
default:
console.log("Invalid day number");
}
Output
Wednesday
Explanation
The switch statement checks the value of dayNumber.
If the value matches case 1, it prints Monday.
If it matches case 2, it prints Tuesday.
If it matches case 3, it prints Wednesday.
Since the value is 3, the third case runs.
The break statement is very important here.
Once a case matches, break stops the program from continuing to the next cases.
If break was missing, the program would continue running the remaining cases as well. This is called fall through behavior.
The default block runs if none of the cases match.
When to use switch vs if else
Both switch and if else help control program flow, but they are suited for slightly different situations.
If else is better when
You are checking ranges like marks greater than 80 or age less than 18
You have complex conditions involving multiple variables
You need logical comparisons like greater than or less than
Switch is better when
You are comparing one variable against multiple fixed values
The cases are clear and predefined such as days of the week or menu options
You want cleaner and more structured code
For example, checking grade ranges is easier with if else.
But printing a day of the week based on a number is usually cleaner with switch.
Understanding when to use each structure helps make your code more readable and maintainable.
Assignment practice
To truly understand control flow, the best approach is to practice writing small programs.
Try implementing the following exercises.
Program 1
Write a program that checks whether a number is positive, negative, or zero.
Example code idea
let number = -5;
if (number > 0) {
console.log("The number is positive.");
} else if (number < 0) {
console.log("The number is negative.");
} else {
console.log("The number is zero.");
}
Output
The number is negative.
In this program we used the else if ladder because there are three possible conditions.
Program 2
Write a program that prints the day of the week using a switch statement.
Example idea
let day = 5;
switch (day) {
case 1:
console.log("Sunday");
break;
case 2:
console.log("Monday");
break;
case 3:
console.log("Tuesday");
break;
case 4:
console.log("Wednesday");
break;
case 5:
console.log("Thursday");
break;
case 6:
console.log("Friday");
break;
case 7:
console.log("Saturday");
break;
default:
console.log("Invalid input");
}
Output
Thursday
We used a switch here because we were comparing a single variable with fixed values representing days of the week.
Final thoughts
Control flow is one of the most important foundations in programming. Without it, a program would simply run every line of code from top to bottom without making any decisions.
Using if, if else, else if ladders, and switch statements allows us to create intelligent programs that respond to different situations.
As you continue learning JavaScript, these concepts will appear everywhere. From validating user input to controlling application behavior, decision making structures form the backbone of real software.
The best way to master them is by experimenting with small examples and observing how the program behaves when the conditions change.
Once you become comfortable with control flow, writing logical and structured programs becomes much easier.
Hope this blog gave the necessary insights needed.



