Sometimes we can have more than two possible scenarios which need a different code to be ran. We can add else if
statements to allow for as many outcomes as we want:
let language = "Python";
if(language == "JavaScript") {
console.log("It's super popular!");
}
else if(language == "Python") {
console.log("It's so simple!");
}
else if (language == "Java") {
console.log("It's pretty hardcore!");
}
else {
console.log("It's exotic!");
}
In the example above, language == "JavaScript"
evaluates to false, so we move on and enter the following else if
.
There, language == "Python"
evaluates to true, so we execute what's inside that block and print "It's so simple!"
.
The rest of the conditions are not evaluated.
In case none of the conditions evaluates to true, then the code in the final else
statement would execute. For example, if language
was "Go"
, the program would have printed "It's exotic!"
Assignment
Follow the Coding Tutorial and let's write some chained conditionals.
Hint
Look at the examples above if you get stuck.
In programming, we often encounter situations where we need to execute different blocks of code based on different conditions. This is where conditional statements come into play. In JavaScript, we can use if
, else if
, and else
statements to handle multiple scenarios. This lesson will focus on chaining conditionals using else if
statements to manage multiple outcomes efficiently.
Before diving into chaining conditionals, it's essential to understand the basic if
statement. An if
statement evaluates a condition inside parentheses. If the condition is true, the code block inside the curly braces is executed. If the condition is false, the code block is skipped.
let number = 10;
if (number > 5) {
console.log("The number is greater than 5");
}
In the example above, the condition number > 5
evaluates to true, so the message "The number is greater than 5" is printed to the console.
Chaining conditionals involves using multiple else if
statements to handle various conditions. The structure is as follows:
if (condition1) {
// code to execute if condition1 is true
} else if (condition2) {
// code to execute if condition2 is true
} else if (condition3) {
// code to execute if condition3 is true
} else {
// code to execute if none of the above conditions are true
}
Each condition is evaluated in order. If a condition is true, the corresponding code block is executed, and the rest of the conditions are skipped.
Let's look at a practical example where we determine the type of a given fruit:
let fruit = "Apple";
if (fruit === "Apple") {
console.log("It's an apple!");
} else if (fruit === "Banana") {
console.log("It's a banana!");
} else if (fruit === "Orange") {
console.log("It's an orange!");
} else {
console.log("Unknown fruit!");
}
In this example, the variable fruit
is compared against different strings. Depending on the value of fruit
, a different message is printed to the console.
When using chained conditionals, it's crucial to ensure that the conditions are mutually exclusive to avoid unexpected behavior. Here are some best practices:
else
as a catch-all for any unspecified conditions.For more complex scenarios, consider using switch statements or even mapping conditions to functions for better readability and maintainability. Here's an example using a switch statement:
let fruit = "Apple";
switch (fruit) {
case "Apple":
console.log("It's an apple!");
break;
case "Banana":
console.log("It's a banana!");
break;
case "Orange":
console.log("It's an orange!");
break;
default:
console.log("Unknown fruit!");
}
Let's implement a function that categorizes a number as positive, negative, or zero using chained conditionals:
function categorizeNumber(number) {
if (number > 0) {
return "Positive";
} else if (number < 0) {
return "Negative";
} else {
return "Zero";
}
}
// Example usage:
console.log(categorizeNumber(10)); // Output: Positive
console.log(categorizeNumber(-5)); // Output: Negative
console.log(categorizeNumber(0)); // Output: Zero
When debugging chained conditionals, ensure that each condition is evaluated correctly. Use console logs to trace the flow of execution. For testing, write test cases to cover all possible scenarios:
function testCategorizeNumber() {
console.assert(categorizeNumber(10) === "Positive", "Test Case 1 Failed");
console.assert(categorizeNumber(-5) === "Negative", "Test Case 2 Failed");
console.assert(categorizeNumber(0) === "Zero", "Test Case 3 Failed");
console.log("All test cases pass");
}
testCategorizeNumber();
When approaching problems involving chained conditionals:
Chaining conditionals is a powerful technique in JavaScript that allows you to handle multiple scenarios efficiently. By understanding the basics, following best practices, and testing thoroughly, you can write clear and maintainable code. Practice with different examples to master this concept and apply it to real-world problems.