JavaScript provides several comparison operators that allow you to compare values. These operators return a boolean (true or false) based on whether the comparison is true or false. This guide will cover the most commonly used comparison operators along with examples.

1. Equal to (==)

The equality operator (==) compares two values for equality, after converting both values to a common type.


Example:
 
let num1 = 5;
let num2 = '5';
console.log(num1 == num2); // Outputs: true
 

In this example, even though num1 is a number and num2 is a string, JavaScript converts num1 to a string before comparing them, resulting in true.


2. Not equal to (!=)

The inequality operator (!=) compares two values for inequality, after converting both values to a common type.


Example:

 
let num1 = 5;
let num2 = '5';
console.log(num1 != num2); // Outputs: false

Here, num1 is equal to num2 in value, so the inequality comparison returns false.


3. Strict equal to (===)

The strict equality operator (===) compares two values for equality, but without type conversion.


Example:

 
let num1 = 5;
let num2 = '5';
console.log(num1 === num2); // Outputs: false

In this case, num1 is a number and num2 is a string, so they are not strictly equal.


4. Strict not equal to (!==)

The strict inequality operator (!==) compares two values for inequality, without type conversion.


Example:
 
let num1 = 5;
let num2 = '5';
console.log(num1 !== num2); // Outputs: true

Here, num1 is a number and num2 is a string, so they are not strictly equal, resulting in true.


5. Greater than (>)

The greater than operator (>) compares whether the left operand is greater than the right operand.


Example:

 
let num1 = 10;
let num2 = 5;
console.log(num1 > num2); // Outputs: true

In this example, num1 is greater than num2, so the comparison returns true.


6. Less than (<)

The less than operator (<) compares whether the left operand is less than the right operand.


Example:
 
let num1 = 5;
let num2 = 10;
console.log(num1 < num2); // Outputs: true

Here, num1 is less than num2, so the comparison returns true.


7. Greater than or equal to (>=)

The greater than or equal to operator (>=) compares whether the left operand is greater than or equal to the right operand.


Example:
 
let num1 = 10;
let num2 = 10;

console.log(num1 >= num2); // Outputs: true

In this case, num1 is equal to num2, so the comparison returns true.


8. Less than or equal to (<=)

The less than or equal to operator (<=) compares whether the left operand is less than or equal to the right operand.


Example:

 
let num1 = 5;
let num2 = 10;
console.log(num1 <= num2); // Outputs: true
 

Here, num1 is less than num2, so the comparison returns true.


Examples of Using Comparison Operators
Example 1: Using Equal to (==)

 
let num1 = 5;
let num2 = '5';
console.log(num1 == num2); // Outputs: true


Example 2: Using Not equal to (!=)

 
let num1 = 5;
let num2 = '5';
console.log(num1 != num2); // Outputs: false


Example 3: Using Strict equal to (===)

let num1 = 5;
let num2 = '5';

console.log(num1 === num2); // Outputs: false


Example 4: Using Greater than (>)

 
let num1 = 10;
let num2 = 5;
console.log(num1 > num2); // Outputs: true


Example 5: Using Less than (<)

 
let num1 = 5;
let num2 = 10;
console.log(num1 < num2); // Outputs: true


Example 6: Using Greater than or equal to (>=)

 
let num1 = 10;
let num2 = 10;

console.log(num1 >= num2); // Outputs: true


Example 7: Using Less than or equal to (<=)
 
let num1 = 5;
let num2 = 10;
console.log(num1 <= num2); // Outputs: true

JavaScript comparison operators are fundamental for comparing values and controlling the flow of your programs. They allow you to make decisions based on whether values are equal, not equal, greater than, less than, or other comparisons.


 



Practice Excercise Practice now