Jasyn Marais

# Logical Operators

15 February 2023

In JavaScript, logical operators are used to combine or modify Boolean values. There are three logical operators in JavaScript:

• `&&`: Logical AND operator
• `||`: Logical OR operator
• `!`: Logical NOT operator

## Logical AND operator (&&)

The logical AND (&&) operator returns true if both operands are true, and false otherwise. Here is an example:

```let a = true;
let b = false;

console.log(a && b); // false
console.log(a && true); // true
```

In this example, the `&&` operator is used to combine two Boolean values (a and b). The result is false, since b is false.

## Logical OR operator (||)

The logical OR (||) operator returns true if at least one operand is true, and false otherwise. Here is an example:

```let a = true;
let b = false;

console.log(a || b); // true
console.log(a || true); // true
console.log(b || false); // false
```

In this example, the `||` operator is used to combine two Boolean values (a and b). The result is true, since a is true.

## Logical NOT operator (!)

The logical NOT (!) operator is used to invert the value of a Boolean expression. It returns true if the expression is false, and false if the expression is true. Here is an example:

```let a = true;

console.log(!a); // false
console.log(!false); // true
```

In this example, the `!` operator is used to invert the value of the a variable, which is true. The result is false.

Logical operators are often used in conditional statements to create more complex conditions. For example:

```let a = 5;
let b = 10;

if (a > 0 && b < 20) {
console.log("a is positive and b is less than 20");
}
```

In this example, the if statement uses the `&&` operator to create a condition that is true only if both a is greater than 0 and b is less than 20. If the condition is true, the message "a is positive and b is less than 20" is logged to the console.

## Double NOT operator (!!)

In JavaScript, the double not operator (!!) can be used to convert a non-boolean value to a boolean value. The conversion is based on the "truthyness" or "falsyness" of the value (see truthiness).

Examples:

```!!"" // false
!!0 // false
!!null // false
!!undefined // false
!!"Hello" // true
!!1 // true
!!{} // true
```