Details
You can nest conditional statements inside of each other. This allows you to check two or more conditions before executing the code in the innner if statement, while also allowing you to execute code in between the conditional statements, or execute code after the inner if statement. Here’s an example:
if (1 > 0) {
// Insert some code before 2nd conditional statement
// ...
if (2 > 1) {
console.log('The first and second conditional statements are true');
}
// Insert some code after the 2nd conditional statement
// ...
}
The example above is an if statement inside of another if statement, but there are other combinations. You could nest a conditional statement inside an else if or else statement, and your nested conditional could also have an else if and/or else statement.
Demo
Exercises
Try the following statements in the console:
if (1 > 0) {
if (2 > 1) {
console.log('The first and second conditional statements are true');
}
}
The output will be “The first and second conditional statements are true”.