Comparison operators allow us to compare values and obtain a boolean result (true or false) based on the relationship between the values.
These operators allow us to compare two values and determine if they are equal, different, greater, or less than, and are fundamental for evaluating expressions and making decisions.
JavaScript’s comparison operators are:
| Operator | Name | Description |
|---|---|---|
== | Equality | Compares if two values are equal |
=== | Strict equality | Compares if two values and their type are identical |
!= | Inequality | Compares if two values are different |
!== | Strict inequality | Compares if two values and their type are identical |
> | Greater than | Verifies if one value is greater than another |
< | Less than | Verifies if one value is less than another |
>= | Greater than or equal to | Verifies if one value is greater than or equal to |
<= | Less than or equal to | Verifies if one value is less than or equal to |
If you want to learn more, check out the Introduction to Programming Course
List of comparison operators
Equality (==)
The equality operator == compares two values and returns true if they are equal, false if they are different. This operator does not consider data types.
5 == 5; // true
5 == '5'; // true, the comparison does not consider the type
Strict equality (===)
The strict equality operator === compares two values and returns true if they are equal and of the same type, false if they are different.
5 === 5; // true
5 === '5'; // false, the types are different
Inequality (!=)
The inequality operator != compares two values and returns true if they are different, false if they are equal. This operator does not consider data types.
5 != 3; // true
5 != '5'; // false, the comparison does not consider the type
Strict inequality (!==)
The strict inequality operator !== compares two values and returns true if they are different or of different types, false if they are equal and of the same type.
5 !== 3; // true
5 !== '5'; // true, the types are different
Greater than (>), greater than or equal to (>=)
These operators compare two numeric values and return true if the first value is greater than (or greater than or equal to) the second, false if it is not.
5 > 3; // true
5 >= 5; // true
Less than (<), less than or equal to (<=)
These operators compare two numeric values and return true if the first value is less than (or less than or equal to) the second, false if it is not.
5 < 10; // true
5 <= 5; // true
It is recommended to use === instead of == (!== instead of !=) to avoid unexpected behaviors due to type conversions.
