🛤️ Controlling Program Flow in JavaScript: if, else, and switch
In JavaScript (and most programming languages), controlling the flow of your program is essential. It helps your code make decisions based on different conditions, and there are several ways to achieve this using conditional statements. The most common ways are through if, else if, else, and switch.
Get Augustocsuarez’s stories in your inbox
Join Medium for free to get updates from this writer.
Let’s explore how these work and when to use them!
🔍 The if Statement
The if statement is used to test a condition. If the condition evaluates to true, the code inside the if block will execute.
Example:
let temperature = 30;
if (temperature > 25) {
console.log("It's hot outside!");
}Here, if the value of temperature is greater than 25, the message "It's hot outside!" will be logged. If the condition is not met, the code inside the if block will not run.
🔄 The else Statement
The else statement allows you to define a block of code that will run if the condition in the if statement is false.
Example:
let temperature = 20;
if (temperature > 25) {
console.log("It's hot outside!");
} else {
console.log("It's cool outside.");
}In this case, since the temperature is 20, the else block will execute and print "It's cool outside.".
🔁 The else if Statement
Sometimes you need to check multiple conditions. In such cases, you can use the else if statement to chain multiple conditions.
Example:
let temperature = 15;
if (temperature > 25) {
console.log("It's hot outside!");
} else if (temperature > 15) {
console.log("It's warm outside.");
} else {
console.log("It's cold outside.");
}Here, we check three conditions:
- If the temperature is above 25, the message
"It's hot outside!"will be printed. - If it’s above 15 but not greater than 25, the message
"It's warm outside."will be printed. - If neither condition is met, it will print
"It's cold outside."
⚖️ The switch Statement
The switch statement is another way to control the flow of your program, especially when you have several possible conditions based on the value of a single variable. It’s often used when there are many potential outcomes, and it can be more readable than chaining multiple if and else if statements.
Example:
let day = "Monday";
switch (day) {
case "Monday":
console.log("It's the start of the week!");
break;
case "Wednesday":
console.log("It's midweek.");
break;
case "Friday":
console.log("The weekend is near!");
break;
default:
console.log("Just another day.");
}In this example:
- If
dayis"Monday", it will print"It's the start of the week!" - If
dayis"Wednesday", it will print"It's midweek." - If
dayis"Friday", it will print"The weekend is near!" - If none of these match, the
defaultcase will print"Just another day."
🛠️ Common Use Cases for Conditional Statements
- Decision Making: You often need to control what happens based on user input or system values.
- Example: Show different messages based on user preferences, such as dark mode or light mode.
- Validation: Before running certain pieces of code, you might want to check if conditions are met.
- Example: Verify if a user has entered valid information into a form before proceeding.
- Game Logic: In gaming applications, conditions determine actions like character health or level progression.
- Example: Check if a player’s health is above or below a threshold to trigger certain events.
🎓 Conclusion
Controlling the flow of your program using conditional statements like if, else, and switch is a fundamental skill in JavaScript. These tools help your code make decisions and respond dynamically to different conditions. Whether you're deciding which message to display or which function to execute, mastering control flow will enable you to write smarter, more efficient programs.
Next time you write JavaScript, think about how you can use these conditional statements to make your code more flexible and adaptable!









