JavaScript Logical Operators

JavaScript Logical Operators are used to perform logical operations and return boolean results. They are commonly used in conditional statements and loops to make decisions based on multiple conditions.

List of Logical Operators:

Operator Description Example Result
&& Logical AND true && false false
` ` Logical OR
! Logical NOT !true false
?? Nullish Coalescing null ?? "default" "default"
?. Optional Chaining obj?.prop undefined

1. Logical AND (&&)

The Logical AND operator returns true if both operands are true. If one or both operands are false, it returns false.

Syntax:

expr1 && expr2

Try It Now

Example:

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

Try It Now

2. Logical OR (||)

The Logical OR operator returns true if at least one of the operands is true. If both operands are false, it returns false.

Syntax:

expr1 || expr2

Try It Now

Example:

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

Try It Now

3. Logical NOT (!)

The Logical NOT operator reverses the truthiness of its operand. If the operand is true, it returns false, and vice versa.

Syntax:

!expr

Try It Now

Example:

let a = true;
console.log(!a); // false
console.log(!false); // true

Try It Now

4. Nullish Coalescing (??)

The Nullish Coalescing operator returns the right-hand operand if the left-hand operand is null or undefined. Otherwise, it returns the left-hand operand.

Syntax:

expr1 ?? expr2

Try It Now

Example:

let a = null;
let b = "default";
console.log(a ?? b); // "default"
console.log("value" ?? b); // "value"

Try It Now

5. Optional Chaining (?.)

The Optional Chaining operator allows safe access to deeply nested properties of an object, returning undefined if the property doesn’t exist, instead of throwing an error.

Syntax:

obj?.prop
obj?.[expr]

Try It Now

Example:

let obj = { name: "John" };
console.log(obj?.name); // "John"
console.log(obj?.age);  // undefined

Try It Now

Combining Logical Operators:

Logical operators can be combined to evaluate complex conditions.

Example:

let age = 25;
let hasLicense = true;

if (age > 18 && hasLicense) {
    console.log("Eligible to drive");
} else {
    console.log("Not eligible to drive");
}
// Output: Eligible to drive

Try It Now

Operator Precedence:

Logical operators have different levels of precedence:

  1. Logical NOT (!): Highest precedence.
  2. Logical AND (&&): Higher than OR but lower than NOT.
  3. Logical OR (||): Lowest precedence among logical operators.

Parentheses can be used to group expressions and control the order of evaluation.

Example:

let result = true || false && !false; 
// Without parentheses, the order of operations is:
// 1. `!false` evaluates to `true`
// 2. `false && true` evaluates to `false`
// 3. `true || false` evaluates to `true`

console.log(result); // true

Try It Now

Summary:

  • && (AND): Both conditions must be true for the result to be true.
  • || (OR): At least one condition must be true for the result to be true.
  • ! (NOT): Inverts the boolean value.
  • ?? (Nullish Coalescing): Returns the right operand if the left is null or undefined.
  • ?. (Optional Chaining): Safely accesses properties that might not exist.

Logical operators are essential for making decisions in JavaScript, allowing for the evaluation of multiple conditions and the implementation of control flow in your code.