In programming, we constantly need to make decisions. Should this code run? Is this user allowed to see that page? To make these decisions, we evaluate conditions, and the fundamental tools for this are logical operators. They are the building blocks of control flow in any language, including TypeScript.
Logical operators work with boolean values (true and false) to produce a single boolean result. Let’s dive into the three core logical operators you’ll use every day: Logical AND (&&), Logical OR (||), and Logical NOT (!).
1. Logical AND (&&)
The Logical AND operator, represented by &&, returns true only if both of its operands are true. If either operand is false, the result will be false.
Think of it like this: “I will go to the park if it is sunny and I have finished my work.” Both conditions must be met.
Truth Table for AND (&&)
| Operand A | Operand B | A && B |
|---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |