javascript javascript-tutorial comparison-operators logical-operators equality strict-equality relational-operators greater-than less-than and-operator
Section 2.3: Operators
Tutorial 2.3.2: Comparison and Logical Operators
Comparison and Logical Operators in JavaScript
Comparison and logical operators allow you to compare values and perform logical operations. They are fundamental for decision-making in JavaScript, such as in if
statements and loops.
1. Comparison Operators
Comparison operators compare two values and return a boolean (true
or false
).
Operator | Symbol | Description | Example | Result |
---|---|---|---|---|
Equal to | == | Checks if values are equal | 5 == '5' | true |
Strict Equal to | === | Checks if values and types are equal | 5 === '5' | false |
Not Equal to | != | Checks if values are not equal | 5 != '5' | false |
Strict Not Equal to | !== | Checks if values and types are not equal | 5 !== '5' | true |
Greater than | > | Checks if left is greater than right | 10 > 5 | true |
Greater than or Equal to | >= | Checks if left is greater than or equal to right | 10 >= 10 | true |
Less than | < | Checks if left is less than right | 3 < 8 | true |
Less than or Equal to | <= | Checks if left is less than or equal to right | 3 <= 3 | true |
Examples of Comparison Operators
console.log(5 == "5"); // true (type coercion)
console.log(5 === "5"); // false (no type coercion)
console.log(10 > 7); // true
console.log(4 <= 4); // true
2. Logical Operators
Logical operators combine multiple conditions or values.
Operator | Symbol | Description | Example | Result |
---|---|---|---|---|
AND | && | Returns true if both conditions are true | true && false | false |
OR | || | Returns true if at least one condition is true | true || false | true |
NOT | ! | Reverses the boolean value | !true | false |
Examples of Logical Operators
// AND operator
console.log(true && true); // true
console.log(true && false); // false
// OR operator
console.log(false || true); // true
console.log(false || false); // false
// NOT operator
console.log(!true); // false
console.log(!false); // true
Combining Comparison and Logical Operators
Use comparison and logical operators together for complex conditions.
Example:
let age = 20;
let hasID = true;
if (age >= 18 && hasID) {
console.log("Access granted.");
} else {
console.log("Access denied.");
}
// Output: "Access granted."
Common Errors
- Confusing
==
with===
:
==
performs type coercion.===
ensures both value and type match.
- Using
&&
or||
incorrectly:
- Logical operators evaluate left to right; ensure your conditions are correct.
Exercise
- Check if a number is greater than 10 and even.
- Write a condition to determine if a user can log in based on the following:
- The user must be logged in (
isLoggedIn
istrue
). - The user's account must not be suspended (
isSuspended
isfalse
).
Example:
let number = 12;
if (number > 10 && number % 2 === 0) {
console.log("The number is greater than 10 and even.");
}
let isLoggedIn = true;
let isSuspended = false;
if (isLoggedIn && !isSuspended) {
console.log("User can log in.");
} else {
console.log("Access denied.");
}
Comments
Please log in to leave a comment.