Understanding How Logical Operators Work in JavaScript


Exploring Logical Operators in JavaScript
Logical operators in JavaScript allow us to combine multiple conditions into a single expression, making our programs more efficient and dynamic. These operators evaluate conditions and return a boolean value—true or false—based on the logic defined. Let’s dive into the details!
What Are Logical Operators?
Logical operators are used to combine expressions and determine their overall truth value. They are:
Logical AND (
&&
)Logical OR (
||
)Logical NOT (
!
)
1. Logical AND (&&
)
The &&
operator checks if both conditions are true. If either condition is false, the result is false.
How It Works:
First Expression: Evaluates to true or false.
Second Expression: Also evaluates to true or false.
Result: True if both are true, false otherwise.
Example:
Code:
let age = 25;
if (age > 18 && age < 30) {
console.log("You are eligible for the program.");
} else {
console.log("You are not eligible.");
}
Output:
You are eligible for the program.
Key Points:
Both conditions (
age > 18
andage < 30
) must be true for the program to run.If one condition fails, the result will be false.
2. Logical OR (||
)
The ||
operator checks if at least one condition is true. If all conditions are false, the result is false.
Example:
Code:
let age = 15;
if (age < 18 || age > 65) {
console.log("You are not eligible for the program.");
} else {
console.log("You are eligible.");
}
Output:
You are not eligible for the program.
Key Points:
The condition will be true even if only one part (
age < 18
orage > 65
) is true.If both are false, the result will be false.
3. Logical NOT (!
)
The !
operator reverses the boolean value of a condition. If the condition is true, !
makes it false, and vice versa.
Example:
Code:
let isStudent = false;
if (!isStudent) {
console.log("You are not a student.");
} else {
console.log("Welcome, student!");
}
Output:
You are not a student.
Key Points:
The
!
operator inverts the logic of the condition.It is particularly useful for negating boolean values.
Real-Life Examples Using Logical Operators
1. Grading System (Logical AND)
Scenario:
A student passes if their marks are ≥ 33.
If marks are ≥ 80, they also get an "A+".
Code:
let marks = 90;
if (marks >= 33 && marks >= 80) {
console.log("Pass");
console.log("Grade: A+");
}
Output:
Pass
Grade: A+
2. Scholarship Eligibility (Logical OR)
Scenario:
- A student qualifies if they score ≥ 90 in Math or Science.
Code:
let mathScore = 85;
let scienceScore = 92;
if (mathScore >= 90 || scienceScore >= 90) {
console.log("You are eligible for the scholarship.");
} else {
console.log("Better luck next time.");
}
Output:
You are eligible for the scholarship.
Combining Logical Operators
Logical operators can also be combined to evaluate more complex conditions.
Example:
Code:
let marks = 75;
if ((marks >= 33 && marks <= 80) || !false) {
console.log("Pass");
} else {
console.log("Fail");
}
Output:
Pass
Tips for Using Logical Operators Effectively
Simplify Conditions: Avoid overly complex expressions by breaking them into smaller, readable parts.
Use Parentheses: When combining multiple operators, use parentheses to clarify precedence.
Test Edge Cases: Always test for boundary conditions, such as the minimum and maximum values.
Subscribe to my newsletter
Read articles from Anmol singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
