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.