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;
return false as both x and y are not same.
=== (strict equal to)
Used to compare both the values and its data type.
For example:
x = 1; y = ’1’; x = y;
returns false as the value is same but data type isn’t.
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;
returns true as x and y are not same.
> (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;
returns true because x which is 6 is greater than 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;
returns true because x which is 5 is smaller than 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;
returns false because x which is 6 is greater than 5 but not equal to.
<= (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;
returns false because x which is 5 is smaller than 6 but not equal to.
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.