JavaScript Comparison and Logical Operators
Learn how comparison and logical operators work in JavaScript. Understand how to compare values, combine conditions, and make decisions in real-world programs using practical examples.
1. What Are Comparison and Logical Operators?
Operators are symbols used to perform operations on values and variables.
Comparison operators compare two values and return a boolean result:
true or false.
Logical operators combine or reverse boolean conditions. They are
commonly used in if statements, loops, validation,
and application business logic.
2. JavaScript Comparison Operators
Comparison operators compare two values and produce a boolean result.
JavaScript provides operators such as ===,
!==, >, and <.
| Operator | Name | Example | Result |
|---|---|---|---|
== |
Loose equality | 5 == "5" |
true |
=== |
Strict equality | 5 === "5" |
false |
!= |
Loose inequality | 5 != "6" |
true |
!== |
Strict inequality | 5 !== "5" |
true |
> |
Greater than | 10 > 5 |
true |
< |
Less than | 3 < 8 |
true |
>= |
Greater than or equal to | 5 >= 5 |
true |
<= |
Less than or equal to | 4 <= 3 |
false |
=== and !== for most comparisons.
They compare values without the type conversion performed by
loose equality operators.
3. Difference Between == and ===
The == operator can convert values to compatible types
before comparing them. The === operator compares both
type and value without performing that type conversion.
Example
let a = 10;
let b = "10";
console.log(a == b);
console.log(a === b);
console.log(a != b);
console.log(a !== b);
Output
true
false
false
true
Explanation:
The first comparison returns true because loose
equality converts the string to a compatible number. Strict equality
returns false because one value is a number and the
other is a string.
4. JavaScript Logical Operators
Logical operators combine conditions or reverse a condition. They are especially useful when a program must check more than one requirement.
| Operator | Name | Meaning | Example |
|---|---|---|---|
&& |
Logical AND | Both conditions must be truthy. | true && true |
|| |
Logical OR | At least one condition must be truthy. | false || true |
! |
Logical NOT | Reverses the truthiness of a value. | !true |
Example
let age = 22;
let hasId = true;
console.log(age >= 18 && hasId);
console.log(age >= 18 || hasId);
console.log(!hasId);
Output
true
true
false
The AND expression is true because both conditions are true. The OR expression is true because at least one condition is true. The NOT operator reverses the boolean value.
5. Logical Operators Truth Table
A truth table shows the result of logical operations for different combinations of boolean values.
| A | B | A && B | A || B | !A |
|---|---|---|---|---|
| true | true | true | true | false |
| true | false | false | true | false |
| false | true | false | true | true |
| false | false | false | false | true |
6. How Logical Conditions Work
Consider a student who can attend an advanced class only if their marks are at least 60 and they have completed the prerequisite.
Condition 1
Marks are at least 60
Logical AND (&&)
Check both conditions
Condition 2
Prerequisite is completed
Final Result
Both conditions must be true
7. Practical Example: Student Eligibility
Let us check whether a student is eligible for an advanced course. The student must score at least 60 marks and complete the prerequisite.
Step 1: Write the Program
let marks = 75;
let prerequisiteCompleted = true;
let eligible = marks >= 60 && prerequisiteCompleted;
console.log("Marks:", marks);
console.log("Prerequisite Completed:", prerequisiteCompleted);
console.log("Eligible:", eligible);
Step 2: Run the Program
node operators.js
Step 3: Expected Output
Marks: 75
Prerequisite Completed: true
Eligible: true
Explanation: The marks condition is true because 75 is greater than or equal to 60. The prerequisite condition is also true. Therefore, the AND operation returns true.
8. Short-Circuit Evaluation
JavaScript logical operators use short-circuit evaluation. For AND, if the left operand is falsy, the right operand is not evaluated. For OR, if the left operand is truthy, the right operand is not evaluated.
Example
console.log(false && "Second value");
console.log(true || "Second value");
console.log(true && "Welcome");
console.log(false || "JavaScript");
Output
false
true
Welcome
JavaScript
Logical AND and OR return operand values, not always boolean values. In the first two expressions, the right-hand operands are skipped. In the last two expressions, the right-hand values are returned.
9. Common Mistakes
| Mistake | Explanation | Correct Approach |
|---|---|---|
Using == everywhere |
Loose equality can convert types. | Prefer === when comparing values and types. |
Using && instead of || |
AND and OR have different meanings. | Choose the operator that matches the requirement. |
Confusing ! with != |
NOT reverses truthiness; inequality compares values. | Understand each operator's purpose. |
| Assuming logical results are always booleans | AND and OR can return operand values. | Check the expression and its operands carefully. |
10. Practice Task
Course Admission Eligibility
Create a JavaScript program that checks course admission eligibility.
- Store a student's marks in a variable.
- Store whether the student completed the prerequisite.
- Use
>=to check the minimum marks. - Use
&&to combine both conditions. - Use
||to check an alternative eligibility condition. - Use
!to reverse a boolean value. - Display the results using
console.log().
Important Points
- Comparison operators produce boolean results.
- Strict equality compares type and value without type conversion.
- Logical AND requires both operands to be truthy.
- Logical OR returns the first truthy operand, or the last operand if none is truthy.
- Logical NOT reverses truthiness.
- Logical operators can skip evaluation through short-circuiting.
- Prefer strict equality for predictable comparisons.
Summary
JavaScript comparison operators compare values, while logical operators combine or reverse conditions. Operators such as ===, !==, >, and < help compare values. The logical operators &&, ||, and ! are useful for validating multiple requirements and controlling program behavior. Understanding strict equality and short-circuit evaluation helps developers write more predictable JavaScript programs.