The logical operators (&&
, ||
, and !
) can be used in combination with the if-else
statement to create more complex conditions and control the flow of execution in a program.
Here's an example of using the &&
operator to evaluate multiple conditions in an if-else
statement:
let score = 75;
let grade;
if (score >= 90 && score <= 100) {
grade = 'A';
} else if (score >= 80 && score < 90) {
grade = 'B';
} else if (score >= 70 && score < 80) {
grade = 'C';
} else {
grade = 'F';
}
console.log('Your grade is ' + grade);
In this example, the &&
operator is used to evaluate two conditions for each of the if
statements. If both conditions are true, then the corresponding code block will be executed and the value of grade
will be assigned. In this case, since score
is equal to 75, the conditions for A
and B
are not met, and the condition for C
is met, so the value of grade
is assigned to "C".
Here's an example of using the ||
operator:
let age = 15;
let canDrive;
if (age >= 16 || age >= 18) {
canDrive = true;
} else {
canDrive = false;
}
console.log('Can you drive? ' + canDrive);
In this example, the ||
operator is used to evaluate two conditions in the if
statement. If either condition is true, then the code block will be executed and the value of canDrive
will be assigned to true
. In this case, since age
is equal to 15, neither condition is met, so the value of canDrive
is assigned to false
.
The !
operator can be used to negate a condition and reverse its value. For example:
let isSunny = false;
let bringUmbrella;
if (!isSunny) {
bringUmbrella = true;
} else {
bringUmbrella = false;
}
console.log('Bring an umbrella? ' + bringUmbrella);
In this example, the !
operator is used to negate the value of isSunny
, which is false
. The result is that the condition in the if
statement is true, and the value of bringUmbrella
is assigned to true
.