Comparison operators
Comparison operators are also called relational operators. These operators are used to construct and test conditions. To use the operator, we need left-hand-side and right-hand-side. The sides represent the values we want to compare and the comparison operator is placed between the values. See the following as an example:
Table 1 lists the 6 comparison operators available in JavaScript. There are six types of comparison operators: equal, not equal to, greater than, less than, greater than or equal to, and less than or equal to. Each of these operators can be used to compare the values of the variables. The result of each of these operators is always either true or false. When using these operators, make sure all of the arguments are of same data type: integers should be compared with integers, strings with strings, and so on.
Table 1 comparison operators | ||
---|---|---|
Operator | Description | |
== | Returns true if both sides are equal. Note the mathematical equal sign (=) is interpreted as an assignment operator in JavaScript. So, in JavaScript, use two equal symbols (==) when you want to find out if one variable is equal to another. | |
!= | Returns true if variables are not equal | |
> | Returns true if the variable on the left is greater than the variable on the right | |
< | Returns true if the variable on the left is less than the variable on the right | |
>= | Returns true if the variable on the left is greater than or equal to the value of the variable on the right | |
<= | Returns true if the variable on the left is less than or equal to the value of the variable on the right |