3.7 Les Opérateurs logiques

Logical operators let you combine multiple conditions into a single decision. JavaScript offers three of them:

  • AND — written && (two ampersands).
  • OR — written || (two vertical bars).
  • NOT — written ! (an exclamation mark).

AND, OR, NOT in action

The && operator returns true if both operands are true. As soon as one operand is false, the whole expression is false. The || operator returns true if at least one operand is true. The ! operator flips a boolean: it turns true into false and vice versa.

Let's model a loan approval rule. The applicant must have a high income and a good credit score to be eligible. If they are eligible we should not refuse their application:

let highIncome = true;
let goodCreditScore = true;

let eligibleForLoan = highIncome && goodCreditScore;
console.log('eligible', eligibleForLoan); // true

let refused = !eligibleForLoan;
console.log('application refused', refused); // false

If both conditions become false (low income and bad credit), eligibleForLoan is false and refused becomes true — exactly the opposite, thanks to the NOT operator. With || instead of &&, the applicant would qualify as soon as one of the two conditions is met.

That's the foundation of logical operators. In the next lesson we'll see how they behave with non-boolean values, which unlocks very useful patterns.

Summary

This lesson introduces JavaScript's three logical operators—AND (&&), OR (||), and NOT (!)—which are essential for making decisions based on multiple conditions. Using a practical loan approval example, the lesson demonstrates how AND returns true only when both operands are true, OR returns true when at least one operand is true, and NOT inverts the boolean value. By combining these operators, developers can create complex conditional logic for real-world applications.

Key points

  • AND operator (&&) returns true only when BOTH conditions are true; if either condition is false, the result is false
  • OR operator (||) returns true when AT LEAST ONE condition is true; only returns false when both conditions are false
  • NOT operator (!) inverts a boolean value—it converts true to false and false to true, useful for negating conditions
  • Logical operators are commonly used in real-world scenarios like eligibility checks (e.g., loan approval requires both high income AND good credit score)
  • Operators can be chained and combined to create complex conditional logic for multi-condition decision-making

FAQ

When should I use AND (&&) vs OR (||)?

Use AND (&&) when ALL conditions must be true (e.g., loan approval requires both high income AND good credit). Use OR (||) when AT LEAST ONE condition must be true (e.g., user qualifies if they have either high income OR excellent credit history).

What does the NOT (!) operator do?

The NOT operator (!) inverts a boolean value. If a condition is true, applying NOT makes it false, and vice versa. For example, if a person is NOT eligible for a loan, the NOT operator converts the eligibility status to the opposite.

Can logical operators be combined together?

Yes, logical operators can be chained and combined. For example, you can write: (highIncome && goodCredit) || excellentSavings. This evaluates which operands are true first, then applies the logical rules to determine the final result.