TypeScript Logical Operators
Learn how to combine conditions using logical AND, logical OR, and logical NOT operators in TypeScript with practical examples and expected outputs.
What Will We Learn?
Logical operators help determine whether one or more conditions are satisfied.
-
Use logical AND with
&&. -
Use logical OR with
||. -
Reverse a boolean value using
!. - Understand short-circuit evaluation.
- Combine logical operators in practical examples.
Logical Operators Workflow
Follow these steps to understand how logical conditions are evaluated.
Step 1
Define ConditionsStore boolean values.
Step 2
Apply OperatorUse AND, OR, or NOT.
Step 3
EvaluateCheck the conditions.
Step 4
Get ResultObtain the result.
1. Logical AND Operator (&&)
The logical AND operator returns true
when both operands are truthy. If either operand
is falsy, the expression short-circuits and returns
a falsy operand.
Example
let hasPaidFees: boolean = true;
let hasSubmittedForm: boolean = true;
console.log(hasPaidFees && hasSubmittedForm);
hasSubmittedForm = false;
console.log(hasPaidFees && hasSubmittedForm);
Expected Output
true
false
The first result is true because both conditions are
true. The second result is false because
hasSubmittedForm is false.
2. Logical OR Operator (||)
The logical OR operator returns true
when at least one operand is truthy. It returns
a falsy result only when both operands are falsy.
Example
let hasJavaKnowledge: boolean = true;
let hasPythonKnowledge: boolean = false;
console.log(hasJavaKnowledge || hasPythonKnowledge);
hasJavaKnowledge = false;
console.log(hasJavaKnowledge || hasPythonKnowledge);
Expected Output
true
false
The first result is true because Java knowledge is available. The second result is false because both conditions are false.
3. Logical NOT Operator (!)
The logical NOT operator reverses a value's truthiness. For boolean values, true becomes false and false becomes true.
Example
let isRegistered: boolean = true;
let isBlocked: boolean = false;
console.log(!isRegistered);
console.log(!isBlocked);
Expected Output
false
true
The NOT operator reverses each boolean value:
!true becomes false and
!false becomes true.
4. Logical Operators Truth Table
A truth table shows the result of logical operators for every combination of boolean inputs.
AND Operator (&&)
| Condition A | Condition B | A && B |
|---|---|---|
| true | true | true |
| true | false | false |
| false | true | false |
| false | false | false |
OR Operator (||)
| Condition A | Condition B | A || B |
|---|---|---|
| true | true | true |
| true | false | true |
| false | true | true |
| false | false | false |
NOT Operator (!)
| Input | !Input |
|---|---|
| true | false |
| false | true |
5. Combining Multiple Conditions
Logical operators can combine comparison expressions to implement application rules.
Example
let marks: number = 75;
let attendance: number = 85;
let hasPassed = marks >= 35;
let hasRequiredAttendance = attendance >= 75;
let isEligible = hasPassed && hasRequiredAttendance;
console.log("Passed: " + hasPassed);
console.log("Attendance Eligible: " + hasRequiredAttendance);
console.log("Eligible: " + isEligible);
Expected Output
Passed: true
Attendance Eligible: true
Eligible: true
The student is eligible because both the marks condition and attendance condition are satisfied.
6. Short-Circuit Evaluation
JavaScript evaluates logical expressions from left
to right. With &&, evaluation
stops when the left operand is falsy. With
||, evaluation stops when the left
operand is truthy.
Example with AND
let isLoggedIn: boolean = false;
function checkPermission(): boolean {
console.log("Checking permission");
return true;
}
let canAccess = isLoggedIn && checkPermission();
console.log("Can Access: " + canAccess);
Expected Output
Can Access: false
Because isLoggedIn is false, the
right-hand function is not executed. This prevents
unnecessary work.
Example with OR
let hasAccess: boolean = true;
function checkBackupAccess(): boolean {
console.log("Checking backup access");
return false;
}
let allowed = hasAccess || checkBackupAccess();
console.log("Allowed: " + allowed);
Expected Output
Allowed: true
Because hasAccess is already true,
the right-hand function is not executed.
7. Logical Operators with if Statements
Logical operators are commonly used in
if statements to decide which
block of code should execute.
Example
let age: number = 22;
let hasValidDocuments: boolean = true;
if (age >= 18 && hasValidDocuments) {
console.log("Registration Allowed");
} else {
console.log("Registration Not Allowed");
}
Expected Output
Registration Allowed
Both conditions are true, so the first block executes.
If either condition becomes false, the
else block executes.
8. Logical Operators Comparison
| Operator | Name | Purpose | Example |
|---|---|---|---|
&& |
Logical AND | Requires both conditions to be true. | a && b |
|| |
Logical OR | Requires at least one condition to be true. | a || b |
! |
Logical NOT | Reverses the truthiness of a value. | !a |
&& and
|| can also return operand values,
not just booleans.
Practical Example: CIIT Course Admission
Let's use logical operators to check whether a student can complete the course admission process.
Example
let hasSubmittedForm: boolean = true;
let hasPaidFees: boolean = true;
let hasValidDocuments: boolean = false;
let isAdmissionComplete =
hasSubmittedForm && hasPaidFees && hasValidDocuments;
let needsFollowUp =
!hasPaidFees || !hasValidDocuments;
console.log("Admission Complete: " + isAdmissionComplete);
console.log("Needs Follow-up: " + needsFollowUp);
Expected Output
Admission Complete: false
Needs Follow-up: true
Admission is not complete because the documents are not valid. The follow-up condition is true because at least one required item is incomplete.
Common Mistakes
-
Confusing AND (
&&) with OR (||). -
Forgetting that NOT (
!) reverses truthiness. -
Using
&&when either condition should be sufficient. - Assuming that logical AND and OR always return booleans.
- Ignoring short-circuit behavior when calling functions.
- Writing complicated conditions that are difficult to read.
Practice Task
Create a TypeScript program for checking student admission eligibility.
- Student age: 21
- Minimum age: 18
- Registration form submitted: true
- Fees paid: false
- Documents verified: true
Use logical AND, OR, and NOT to calculate whether the student meets the age requirement, whether admission is complete, and whether follow-up is required.
Hint
let age = 21;
let minimumAge = 18;
let formSubmitted = true;
let feesPaid = false;
let documentsVerified = true;
let ageEligible = age >= minimumAge;
let admissionComplete =
formSubmitted && feesPaid && documentsVerified;
let followUpRequired =
!feesPaid || !documentsVerified;
console.log(ageEligible);
console.log(admissionComplete);
console.log(followUpRequired);
Expected Output
true
false
true
Summary
In this lesson, we learned about logical AND, logical OR, and logical NOT operators. We also covered truth tables, short-circuit evaluation, and combining conditions in practical TypeScript programs.
Key Point:
Use && when both conditions must
be satisfied, || when at least one condition
is sufficient, and ! to reverse truthiness.