JavaScript Introduction | Operators | Comparison Operators
Comparison Operator
Comparison operators compare the relative size or equality of operands and return true or false. All comparison operators are binary operators with two operands, and operands are evaluated from left to right.
| Comparison Operator | Description |
|---|---|
| == | Returns true if the value of the left operand is equal to the value of the right operand. |
| === | Returns true if the left and right operands have the same value and the same type. |
| != | Returns true if the value of the left operand is not equal to the value of the right operand. |
| !== | Returns true if the left and right operands do not have the same value or do not have the same type. |
| > | Returns true if the value of the left operand is greater than the value of the right operand. |
| >= | Returns true if the value of the left operand is greater than or equal to the value of the right operand. |
| < | Returns true if the value of the left operand is less than the value of the right operand. |
| <= | Returns true if the value of the left operand is less than or equal to the value of the right operand. |
In JavaScript, comparison operators compare operands by two criteria depending on their types.
- If both operands are numbers, the numbers are compared with each other.
- If both operands are strings, they are compared alphabetically from the first character.
var x = 3, y = 5;
var a = "abc", b = "bcd";
document.write((x > y) + "<br>"); // false because the value of y is greater than the value of x
document.write((a <= b) + "<br>"); // 'a' comes before 'b' alphabetically, so 'a' is less than 'b'.
document.write(x < a); // x is a number and a is a string, so they cannot be compared.
In the third operation above, the value of variable x is a number, while the value of variable a is a string. The comparison operator < returns true only when the value of x is less than the value of a; in all other cases it returns false. Therefore, when different types cannot be compared, the condition for true cannot be satisfied, so the result is always false.
Equality Operator and Strict Equality Operator
The equality operator (==) and strict equality operator (===) both compare whether two operands are equal. Both operators compare operands regardless of type, but their criteria for defining equality are slightly different.
The equality operator (==) returns true if the values of the two operands are equal. If the operands have different types, their types are forcibly converted to the same type for comparison. The strict equality operator (===), however, returns true only when the two operands have the same value and the same type, without type conversion.
var x = 3, y = '3', z = 3;
document.write((x == y) + "<br>"); // true because x and y are converted to the same type before comparison
document.write((x === y) + "<br>"); // false because x and y have different types
document.write(x === z); // true because x and z have the same value and type
The inequality operator (!=) and strict inequality operator (!==) work exactly opposite to the equality and strict equality comparisons, respectively.