JavaScript comparison and logical operators are used to compare values and combine conditions. They always return a boolean value: true or false. These operators are heavily used in if statements, loops, and other control flow logic.
&&, ||, and !.===) checks both value and data type.true or false.Comparison operators compare two values and return a boolean result.
| Operator | Description | Example | Result |
|---|---|---|---|
== |
Equal to (loose, allows type coercion) | 5 == "5" |
true |
=== |
Equal value and type (strict) | 5 === "5" |
false |
!= |
Not equal (loose) | 5 != "5" |
false |
!== |
Not equal value or type (strict) | 5 !== "5" |
true |
> |
Greater than | 8 > 5 |
true |
< |
Less than | 3 < 5 |
true |
>= |
Greater than or equal to | 10 >= 10 |
true |
<= |
Less than or equal to | 4 <= 3 |
false |
Logical operators combine boolean expressions to build complex conditions.
| Operator | Name | Description | Example |
|---|---|---|---|
&& |
Logical AND | Returns true if both conditions are true |
(x > 5 && y < 10) |
|| |
Logical OR | Returns true if at least one condition is true |
(x > 5 || y < 10) |
! |
Logical NOT | Reverses the result of a condition | !(x > 5) |
console.log(5 == "5"); // true (values equal, types differ)
console.log(5 === "5"); // false (number vs string)
console.log(10 > 7); // true
console.log(4 <= 3); // false
let a = 6;
let b = 3;
console.log(a > 5 && b < 5); // true (both conditions are true)
console.log(a < 5 || b < 2); // false (both conditions are false)
console.log(!(a == b)); // true (a == b is false, !false = true)
5 == "5" ➝ true (values are equal, types are ignored).5 === "5" ➝ false (number is not the same type as string).10 > 7 ➝ true (10 is greater than 7).4 <= 3 ➝ false (4 is not less than or equal to 3).a > 5 && b < 5 ➝ true because both parts are true.a < 5 || b < 2 ➝ false because both parts are false.!(a == b) ➝ true because a == b is false, and NOT of false is true.=== and !== over == and != to avoid unexpected type coercion.if, while, and other control structures.a || b skips evaluating b if a is already true.a && b skips evaluating b if a is false.=) with comparison (== / ===).== and ===. Observe how the result changes when one is a number and the other is a string.if statement that uses && to check two conditions (for example, age range: age >= 18 && age <= 60).|| that prints a message if either of two conditions is true (for example, discount for students or senior citizens).! with an expression like !(score >= 50) and log both the original and negated results to understand how NOT works.