Hello everyone, welcome to my blog, In this blog post, we ‘ll be learning about conditionals in JavaScript. Conditional statements are one of the most important tools in programming, allowing code to execute specific actions based on whether a condition is true or false. They’re basically the code that makes decisions. This makes our code more versatile and dynamic. In this post, we will explore if-else statements, else-if statements, and switch statements, explaining when and how to use them effectively.
If-Else Statements
An if-else statement is the simplest form of conditional logic. It allows a program to choose between two possible actions based on a condition being either true or false.
Example:
A real-life example of an if-else statement is determining whether a student passes a test. In JavaScript, this logic would look like this:
let result = 50;
if (result > 40) {
console.log("You passed the test");
} else {
console.log("You did not pass the test");
}
Explanation:
- The variable
resultis assigned a value of50. - The
ifcondition checks ifresultis greater than40. - If true, it prints “You passed the test”.
- If false, it executes the
elseblock, printing “You did not pass the test”.
Now, if we change result to 30 and rerun the code, the output will be:
You did not pass the test
since 30 is not greater than 40.
Else-If Statements
Sometimes, we need to check multiple conditions instead of just two outcomes. Else-if statements allow us to evaluate several possibilities.
Example:
Consider a sports competition where participants receive different medals based on their rank:
let place = "First";
if (place === "First") {
console.log("Gold");
} else if (place === "Second") {
console.log("Silver");
} else if (place === "Third") {
console.log("Bronze");
} else {
console.log("No medal");
}
Explanation:
- If
placeis First, it prints “Gold”. - If
placeis Second, it prints “Silver”. - If
placeis Third, it prints “Bronze”. - If none of the above conditions are met, the
elseblock prints “No medal”.
While else-if statements work well, they can become cumbersome if there are too many conditions. For such cases, we use switch statements.
Switch Statements
A switch statement provides a cleaner way to handle multiple conditions without writing multiple else-if blocks.
Example:
Rewriting the medal standings example using a switch statement:
let place = "First";
switch (place) {
case "First":
console.log("Gold");
break;
case "Second":
console.log("Silver");
break;
case "Third":
console.log("Bronze");
break;
default:
console.log("No medal");
}
Explanation:
- The
switchchecks the value ofplace. - If
placeis First, it prints “Gold” and stops further execution withbreak. - If
placeis Second, it prints “Silver”. - If
placeis Third, it prints “Bronze”. - If no conditions match, the
defaultblock prints “No medal”.
When to Use If-Else vs. Switch
| Use Case | If-Else Statement | Switch Statement |
|---|---|---|
| Few conditions | ✅ Best choice | ❌ Unnecessary |
| Complex logical checks | ✅ Can handle | ❌ Limited to exact values |
| Many specific values | ❌ Gets messy | ✅ Cleaner solution |
- Use if-else statements when dealing with fewer conditions or complex logical expressions.
- Use a switch statement when checking against many specific values for better readability.
Conclusion
Congratulations! You have learned how to use if-else, else-if, and switch statements to make decisions in programming. These structures allow your code to handle different conditions efficiently.
If you want to learn more, check out our JavaScript playlist for deeper insights. Thanks for reading, and happy coding!
