A nested conditional statement is an if
or if-else
statement inside another if-else
statement:
let isSunny = true;
let temp = 100;
if (isSunny) {
if(temp > 100) {
console.log("Going to the beach");
}
else {
console.log("Going to the park");
}
}
else {
console.log("Learning to code");
}
The code above prints "Going to the park"
.
Assignment
Follow the Coding Tutorial and let's write some nested conditional statements.
Hint
Look at the examples above if you get stuck.
In this lesson, we will explore nested conditionals in JavaScript. Nested conditionals are a powerful tool that allows you to make more complex decisions in your code. They are particularly useful in scenarios where multiple conditions need to be checked before executing a block of code.
Before diving into nested conditionals, it's important to understand the basic if
and if-else
statements. These statements allow you to execute code based on whether a condition is true or false.
let isSunny = true;
if (isSunny) {
console.log("It's sunny outside!");
} else {
console.log("It's not sunny outside.");
}
In the example above, the message "It's sunny outside!" will be printed if isSunny
is true. Otherwise, "It's not sunny outside." will be printed.
Nested conditionals involve placing an if
or if-else
statement inside another if-else
statement. This allows you to check multiple conditions in a hierarchical manner.
let isSunny = true;
let temp = 100;
if (isSunny) {
if (temp > 100) {
console.log("Going to the beach");
} else {
console.log("Going to the park");
}
} else {
console.log("Learning to code");
}
In this example, the outer if
statement checks if isSunny
is true. If it is, the inner if-else
statement checks the value of temp
to decide whether to go to the beach or the park. If isSunny
is false, the code will print "Learning to code".
Let's look at a few more examples to understand nested conditionals better.
let isWeekend = true;
let isSunny = false;
if (isWeekend) {
if (isSunny) {
console.log("Going hiking");
} else {
console.log("Staying home and reading a book");
}
} else {
console.log("Going to work");
}
In this example, the code checks if it's the weekend. If it is, it further checks if it's sunny to decide between hiking and reading a book. If it's not the weekend, the code prints "Going to work".
When using nested conditionals, it's easy to make mistakes such as forgetting to close braces or misplacing conditions. Here are some best practices:
As you become more comfortable with nested conditionals, you can start combining them with other control structures like loops and switch statements for more complex logic.
let day = "Saturday";
let isSunny = true;
switch (day) {
case "Saturday":
case "Sunday":
if (isSunny) {
console.log("Going to the beach");
} else {
console.log("Staying home");
}
break;
default:
console.log("Going to work");
}
In this example, we use a switch statement to check the day of the week and then use a nested conditional to decide what to do based on the weather.
Here is a well-commented code snippet demonstrating nested conditionals:
let isHoliday = true;
let isSunny = false;
let temp = 85;
if (isHoliday) {
// Check if it's sunny
if (isSunny) {
// Check the temperature
if (temp > 90) {
console.log("Going to the beach");
} else {
console.log("Going to the park");
}
} else {
console.log("Staying home and watching movies");
}
} else {
console.log("Going to work");
}
When debugging nested conditionals, use console.log statements to print the values of variables and conditions. This helps you understand the flow of your code.
let isHoliday = true;
let isSunny = false;
let temp = 85;
if (isHoliday) {
console.log("It's a holiday");
if (isSunny) {
console.log("It's sunny");
if (temp > 90) {
console.log("Going to the beach");
} else {
console.log("Going to the park");
}
} else {
console.log("It's not sunny");
console.log("Staying home and watching movies");
}
} else {
console.log("It's not a holiday");
console.log("Going to work");
}
Use testing frameworks like Jest to write tests for your functions. This ensures your code works as expected.
When approaching problems involving nested conditionals:
Nested conditionals are a fundamental concept in programming that allow you to handle complex decision-making processes. By mastering nested conditionals, you can write more efficient and readable code. Practice writing nested conditionals in different scenarios to become more comfortable with them.