Control Flow in JavaScript: If, Else, and Switch Explained
What control flow means in programming ?
Control flow statements are termed as the fundamentals of programming languages that allow the developers to control the order in which instructions are executed in a program. They enable the execution of a block of code multiple times, executing a block of code based on conditions, terminate or skips execution of certain lines of code, etc.
programs typically follow a sequential flow from top to bottom, there are scenarios where we need more flexibility. Normally Code is running in the order starts from the first line in the file and ends to the last line, unless the code runs across these types of structures that change control flow, these structure are conditionals and loops.
For example when you go to a hotel and order “Dal-baati” and then addon “chaach,”Hotel's kitchen doesn't prepare entire menu for you. It prepares only specific items that you have ordered. The decision is based on your request.
Similarly, in programming, the computer does not execute every possible line blindly. It follows instructions based on conditions and logic. If a condition is true, that specific block runs. If it is false, it skips it. Just like hotel prepares only what you ordered, program executes only required instructions based on control flow.
if Statement
If statements allow you to make decisions about which code block requires to run, so it can run different blocks on based of condition. if statement runs a block of code if the condition is true.
syntax:
if (condition){
// code Block executes if condition is true}
condition: a type of expression that is considered as to be a truthy or falsy.
for example, Imagine someone goes to apply for a driving license. The authority simply checks one condition — is the person older than 18? If yes, they will say, “You are eligible to drive.” If not, they do not allow it.
let age = 19;
if (age > 18)
console.log("Congratulations, You are eligible to drive");
if-else Statement
The if-else statement allows you to execute one block of code if the condition is true and another block of else will execute in case that condition is false. It's a way to handle two possible outcomes based on a condition.
syntax:
if (condition){
// code block for true condtion
} else {
// code block for false condition}
for example Imagine you are plugging your phone to charge. The system checks battery level first. If battery is already full, it shows Battery is full. Otherwise, it starts charging and shows charging. This is how if-else statement works. it checks a condition and performs an action based on whether the condition is true or false.
let battery = 78
if (battery === 100) {
console.log("Battery is full");
} else {
console.log("Charging"); // this code block will run
}
Else-if ladder
The if-else-if ladder is used to check multiple conditions in one sequence. Once a true condition is found, its block will executes, and the rest are skipped. Use else if statement to specify a new condition from multiple condition if the first is false.
syntax:
if (condition1) {
// runs if condition1 is true
} else if (condition2) {
// runs if condition2 is true
} else if (condition3) {
// runs if condition3 is true
} else{
// runs if all conditions are false
}
for example Imagine you are getting ready to go outside. Before choosing your outfit, you check what weather is outside ?. If it is cold, you will wear a jacket. If it is rainy, you take an umbrella. Otherwise, you can wear light clothes. Your decision depends completely on the weather condition, just like an if-else statement makes decisions based on conditions.
let weather = "rainy"
if (weather === "cold") {
console.log("Wear a jacket")
} else if (weather === "rainy") {
console.log("Take umbrella with you") // this block code will run
} else {
console.log("Wear light clothes")
}
Switch statement
The switch statement evaluates an expression and executes code based on case match. It’s an efficient alternative to multiple if-else statements, improving readability when handling many conditions. Break stops execution within switch block. Default Runs if no cases match. It’s optional but provides a fallback option.
syntax:
switch (expression) {
case value1:
// code block
break;
case value2:
// code block
break;
default:
// code if no case matches
}
Imagine you enter a number to represent a day. The system directly matches that number to its corresponding day, such as 1 for Monday or 3 for Wednesday. If the number does not match any valid option from 1 to 7, it shows “Invalid day number.” This is how a switch statement works — it selects the correct case based on one value.
let dayNumber = 3
switch (dayNumber) {
case 1:
console.log("Monday")
break
case 2:
console.log("Tuesday")
break
case 3:
console.log("Wednesday") // this code will run
break // program will out from here
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 number")
}
Which control structure should be use and why ?
The choice of control structure depend on problem what are you solving. If you required to make decision based on some condition or ranges, an if–else statement is usually the best option because it is flexible and it can handle complex logic easily. If you are comparing one variable with many fixed values like days of week or menu options, switch statement is more clean and easy to read for most cases. When you need to repeat a task many times, loops like for or while should be used because they are help in repetition work. In real practice, if–else and loops are mostly used since they covers wide range of situations, while switch is preferred when choices are clearly defined and limited in number.




