Code highlights logo

Learn

Conditional Statements

Logical Operators

Logical operators are used to combine multiple boolean expressions and determine the overall truth or falsehood of the combined expression. They are particularly useful for making decisions based on multiple conditions.

There are three main logical operators: AND, OR, and NOT.

  • AND Operator (&&): The AND operator returns true if both of its operand expressions are true. Otherwise, it returns false.
    1if (age >= 18 && hasValidLicense) {
    2 console.log("You can drive a car.");
    3}
  • OR Operator (||): The OR operator returns true if at least one of its operand expressions is true. If both expressions are false, it returns false.
    1if (isWeekend || isHoliday) {
    2 console.log("It's time to relax.");
    3}
  • NOT Operator (!): The NOT operator returns the opposite boolean value of its operand expression. If the expression is true, it returns false. If the expression is false, it returns true.
    1if (!isRaining) {
    2 console.log("It's a sunny day!");
    3}

You can also combine multiple logical operators to create more complex conditions.

Remember to use parentheses () when combining logical operators to control the order of evaluation.

Now that we understand logical operators, we can use them within conditional statements to make more nuanced decisions in our code.


Instructions

1.

Create a variable result1 and assign the value of x less than or equal to y using the <= operator.

2.

Create a variable result2 and assign the value of y greater than x and z using the && operator.

Sign up to start coding

Already have an account?

Sign In

Course content