A type of operator which compares two input values, called operands.
Expressions using this type of operator always return one of two values: true or false.
For example, if you were to ask if the number 2, the same as the number 3, 2 = 3
? This would return false.
If instead you were to ask if the number 2 is less than the number 3 2 < 3
, this would return true.
In these cases: 2 and 3 are the operands, and = and < are the operators.
Although they vary in how they are written between languages, the comparison operators are roughly as follows:
Operator | Example | Description |
---|---|---|
== | 1 == 2 returns false | are these values equal to one another |
=== | 2 === ‘2’ returns false | are these values equal to one another and do they have the same type |
!= | 1 != 2 returns true | are these values not equal to one another |
!== | 1 !== ‘1’ returns true | are these values not equal to one another and do they have a different type |
> | 1 > 2 returns false | is the left value greater than the right value |
< | 1 < 2 returns true | is the left value less than the right value |
>= | 3 >= 2 returns true | is the left value greater than or equal to the right value |
<= | 1 <= 2 returns true | is the left value less than or equal to the right value |
In some languages there is another comparison operator known as a ternary operator which requires 2 operands and 2 statements. For example 1 > 2 ? "1 is more than 2" : "1 is less than or equal to 2"
. This will return ‘1 is less than or equal to 2’ but it will not evaluate the statement (1 > 2
) itself.