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

What Control Flow Means in Programming
In everyday life, we constantly make decisions. If it’s raining, we take an umbrella. If it’s late, we hurry. If it’s the weekend, we relax. Programming works in a similar way.
Control flow is simply the way a program decides which code should run and which should not. Instead of executing every line blindly, a program checks conditions and chooses a path based on the result.
In JavaScript, the most common tools for making decisions are if, else, and switch.
The if Statement
The if statement is the simplest way to make a decision in code. It checks whether a condition is true. If it is, the code inside the block runs.
Imagine checking whether a number is positive.
let number = 10;
if (number > 0) {
console.log("The number is positive");
}
Here’s how the program runs step by step. First, JavaScript evaluates the condition number > 0. Since 10 is greater than 0, the condition becomes true. Because the condition is true, the message inside the block is printed.
If the condition were false, the code inside the block would simply be skipped.
The if-else Statement
Sometimes we want the program to do something different when the condition is false. That’s where else comes in.
let age = 16;
if (age >= 18) {
console.log("You can vote");
} else {
console.log("You cannot vote yet");
}
In this example, the program first checks if the age is 18 or more. If the condition is true, it prints that the person can vote. Otherwise, the code inside the else block runs.
This allows the program to choose between two possible paths.
The else if Ladder
Sometimes a program needs to check multiple conditions. In that case, we can use an else if ladder.
Let’s write a program that checks whether a number is positive, negative, or zero.
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");
}
Here’s how this works. The program first checks if the number is greater than zero. If not, it moves to the next condition and checks if the number is less than zero. If neither condition is true, the program runs the final else block.
This step-by-step checking continues until one condition matches.
The switch Statement
The switch statement is another way to handle multiple possible outcomes. It is often used when a variable can have several known values.
Let’s write a program that prints the 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;
case 6:
console.log("Saturday");
break;
case 7:
console.log("Sunday");
break;
default:
console.log("Invalid day");
}
The program compares the value of day with each case. When it finds a match, that block runs.
The break statement is very important here. It stops the program from continuing to the next case. Without break, JavaScript would keep running the remaining cases even after finding a match.
When to Use Switch vs If-Else
Both structures help control program flow, but they are used in slightly different situations.
The if-else structure is useful when conditions involve comparisons like greater than, less than, or complex logical checks.
The switch statement works best when a variable is compared against many fixed values, such as menu options, days of the week, or status codes.
Choosing the right structure makes your code easier to read and understand.






