JavaScript Comparison Operators


What Are Comparison Operators?

Comparison operators are used to compare two values. They return a Boolean value (true or false) based on the comparison result.

These operators are commonly used in conditional statements, loops, and decision-making code.


List of Comparison Operators

Operator Name Description Example Output
== Equal to Compares values (type conversion allowed) 5 == "5" true
=== Strict equal to Compares values and types 5 === "5" false
!= Not equal to Compares values (type conversion allowed) 5 != "5" false
!== Strict not equal to Compares values and types 5 !== "5" true
> Greater than Checks if left is greater than right 10 > 7 true
<< /td> Less than Checks if left is less than right 3 < 8 true
>= Greater than or equal Checks if left is greater than or equal to right 6 >= 6 true
<=< /td> Less than or equal Checks if left is less than or equal to right 4 <= 5 true

1. Equal to (==)

  • Compares only values, not types.
  • Converts data types if needed (type coercion).
console.log(5 == "5"); // Output: true

2. Strict Equal to (===)

  • Compares both value and type.
  • No type conversion is performed.
console.log(5 === "5"); // Output: false

3. Not Equal to (!=)

  • Checks if values are not equal, ignoring data type.
console.log(10 != "10"); // Output: false

4. Strict Not Equal to (!==)

  • Checks if values or types are not equal.
console.log(10 !== "10"); // Output: true

5. Greater Than (>)

console.log(8 > 5); // Output: true

6. Less Than (<)< /h3>
console.log(3 < 9); // Output: true

7. Greater Than or Equal (>=)

console.log(10 >= 10); // Output: true

8. Less Than or Equal (<=)< /h3>
console.log(5 <= 6); // Output: true

Comparing Strings

Strings are compared based on Unicode values (alphabetical order).

console.log("apple" < "banana"); // Output: true
console.log("cat" > "bat"); // Output: true

Comparing Different Types

JavaScript will try to convert data types implicitly when using == , but not with === .

console.log("5" == 5); // Output: true
console.log("5" === 5); // Output: false

Use Case in Conditions

let age = 18;
if (age >= 18) {
console.log("Eligible to vote");
} else {
console.log("Not eligible");
}
// Output: Eligible to vote

Recap: == vs ===

Operator Checks Value Checks Type Example Output
== Yes No 5 == "5" true
=== Yes Yes 5 === "5" false