Comparison Operators
The operator used to compare two value and identify the difference between two value is known as comparison operator. Comparison operators in JavaScript is used to compare two values of any type be it text-based, or number-based. They compare the two values and return the boolean expression as the output. There are seven types of comparison operators:
Types of Operators
- == (equal to)
- === (strict equal to)
- != (not equal to)
- > (greater than)
- < (less than)
- >= (greater than or equal to)
- <= (greater than or equal to)
Let’s discuss about each on them in-depth with an example:
== (equal to)
Used to compare two values, returns true if both the values are same, else false. For example:
x = 1;
y = 2;
x = y;
=== (strict equal to)
Used to compare both the values and its data type.
For example:
x = 1;
y = ’1’;
x = y;
Do read about the 4 Key Differences Between == and === Operators in JavaScript to understand in depth.
!= (not equal to)
Pronounced as “not equals to”, used to compare when both the values should not be same. For example:
x = 5;
y = 6;
x != y;
> (greater than)
This symbol denotes greater than value. When you compare two values and one variable is greater than the other.
For example:
let x = 6;
x > 5;
< (less than)
This symbol denotes smaller than value. When you compare two values and one variable is smaller than the other.
For example:
let x = 5;
x < 6;
>= (greater than or equal to)
This symbol denotes greater than or equal to value. When you compare two values and one variable is greater than or equal to the other.
For example:
let x = 6;
x >= 5;
<= (less than or equal to)
This symbol denotes smaller than or equal to value. When you compare two values and one variable is smaller than or equal to the other.
For example:
let x = 5;
x <= 6;
Conclusion
You’ve read about the comparison operators in JavaScript, its types with some well-explained examples to help you understand the concept in a more deeper way. Let’s move ahead to read about the other types of operators in the next article.