JAVASCRIPT Tutorial

Logical Operators

Logical operators play a crucial role in programming and are used to combine Boolean expressions and control program flow.

Key Concepts

  • Boolean Logic: Deals with true/false values.
  • Logical Operators:
    • AND (&&): True only if both operands are true.
    • OR (||): True if either operand is true.
    • NOT (!): Reverses the truthiness of an operand.

Practical Steps

  1. Identify Boolean Expressions: Boolean expressions evaluate to true or false.
  2. Use Logical Operators: Connect Boolean expressions using AND, OR, and NOT to form more complex conditions.
  3. Evaluate Result: The result of the logical operation is also a Boolean value (true or false).

Combining Expressions

Logical operators allow you to combine multiple Boolean expressions into a single expression.

(condition1 && condition2) || condition3

This expression is true if either condition3 is true, or both condition1 and condition2 are true.

JavaScript Example

let isLoggedIn = true;
let hasPermission = false;

if (isLoggedIn && hasPermission) {
  console.log("Access granted");
} else {
  console.log("Access denied");
}

In this example, the && operator ensures that both isLoggedIn and hasPermission are true for access to be granted, demonstrating the practical use of logical operators in conditional statements.