# Compound If Statements

There are two logical operators:

&& - AND
|| - OR
1
2

We can use these inside our if statements to run multiple tests conditions.

You can programmatically ask questions like "is your age over 18 AND do you like to drink?".

let beverage = "Corona";
let age = 19;
if (age > 18 && beverage == "Corona") {
  console.log("You are over 18 and drink Corona");
} else {
  console.log("You are not over 18 OR your beverage is not Corona");
}
1
2
3
4
5
6
7

The alternative to using the logical operators && or || is to use nested if statements. Here is the alternate version of the above code, using nested if statements.

let beverage = "Corona";
let age = 19;
if (age > 18) {
  if (beverage == "Corona") {
    console.log("You are over 18 and drink Corona");
  } else {
    console.log("You are over 18 but do NOT drink Corona");
  }
} else {
  console.log("you are not over 18");
}
1
2
3
4
5
6
7
8
9
10
11

So, depending what you want to do with the result either could be the appropriate choice for your code.

Back to Week 3 main page

Last Updated: 6/13/2020, 11:30:19 PM