Logical Operators

When you need to evaluate complex rules that involve multiple true/false conditions, you use logical operators.

The Operators

While some languages (like Python) use English keywords like and or or, JavaScript uses symbolic syntax—similar to Go, Rust, and C.

  • && (AND): Returns true only if both conditions are true.
  • || (OR): Returns true if either of the conditions is true.
  • ! (NOT): Flips the boolean value. Returns true if the input is false.

Truth Tables

true && true;   // true
true && false;  // false

true || false;  // true
false || false; // false

!false;         // true
!true;          // false

Practical Example

You can combine these operators to build complex validation rules. You can also use parentheses () to explicitly define the order of operations, just like in standard math.

const hasHighOpenRate = true;
const isRecent = true;
const hasStrongReplyRate = false;
const canBeResent = true;
const isFlaggedAsSpam = false;

// A campaign is high engagement if it is not spam, has a high open rate,
// is recent, and either has strong replies OR can be resent.
const isHighEngagement = !isFlaggedAsSpam && hasHighOpenRate && isRecent && (hasStrongReplyRate || canBeResent);

console.log(`The campaign is high-engagement: ${isHighEngagement}`);