Imagine you are driving a car and you come to a crossroad. Do you go left, right, or straight? Your decision depends on your destination. In programming, we call this Control Flow.
By default, JavaScript reads code from top to bottom. But what if you only want a specific block of code to run under certain conditions? That’s where control structures like if, else, and switch come in.
What is Control Flow?
Control flow is the order in which individual statements, instructions, or function calls are executed. Think of it as the GPS for your code. It tells the computer: "If this happens, do that; otherwise, do this instead."
Without control flow, your programs would be very boring—they would do the exact same thing every single time.
1. The if Statement: The Basic Gatekeeper
The if statement is the most fundamental way to control flow. It checks a condition; if that condition is true, the code inside the curly braces runs.
JavaScript
let age = 20;
if (age >= 18) {
console.log("You are eligible to vote!");
}
Real-life Example: If you have money in your wallet, you can buy a snack. If not, you just keep walking.
2. The if-else Statement: Having a Plan B
What if the condition is false? The else block allows you to provide an alternative action.
JavaScript
let isRaining = false;
if (isRaining) {
console.log("Take an umbrella.");
} else {
console.log("Enjoy the sunshine!");
}
3. The else if Ladder: Handling Multiple Choices
Sometimes life isn't just "Yes" or "No." When you have multiple specific conditions to check, you use an else if ladder.
JavaScript
let marks = 85;
if (marks >= 90) {
console.log("Grade: A+");
} else if (marks >= 75) {
console.log("Grade: A"); // This runs!
} else if (marks >= 60) {
console.log("Grade: B");
} else {
console.log("Grade: C");
}
How it works: JavaScript checks each condition one by one. As soon as it finds one that is true, it executes that block and skips the rest of the ladder.
4. The switch Statement: The Multi-Choice Menu
If you find yourself writing a lot of else if statements to check the exact same variable for different values, a switch statement is much cleaner.
Think of it like an elevator. You press a button for a specific floor (the case), and the elevator takes you exactly there.
JavaScript
let fruit = "Apple";
switch (fruit) {
case "Banana":
console.log("Bananas are $1 per pound.");
break;
case "Apple":
console.log("Apples are $2 per pound.");
break;
default:
console.log("Sorry, we are out of that fruit.");
}
The Importance of break
Inside a switch, the break keyword is vital. Without it, the code will continue to execute the next cases even if they don't match (this is called "falling through").
When to use switch vs. if-else?
This is a common interview question! Here is the simple rule of thumb:
Feature | if-else | switch |
Logic | Best for ranges (e.g., | Best for fixed, discrete values (e.g., |
Readability | Can get messy with too many conditions. | Very clean for 5+ specific cases. |
Performance | Slightly slower for massive lists of conditions. | Slightly faster as it maps directly to cases. |
Conclusion
Control flow is the backbone of logical programming. Mastering if-else and switch allows you to create dynamic, responsive applications that can handle any input.
Start by keeping your conditions simple, and as you get comfortable, you'll find yourself naturally knowing which structure to reach for in every situation!
