You know comparison operators from math class, but let’s refresh your knowledge:
Greater than operator (a > b) returns true if the left operand is greater than the right operand.
a > b
Example of the greater than operator:
Less than operator (a < b) returns true if the left operand is less than the right operand.
a < b
Example of the less than operator:
Greater than or equal operator (a >= b) returns true if the left operand is greater than or equal to the right operand.
a >= b
Example of the greater than or equal operator:
Less than or equal operator (a <= b) returns true if the left operand is less than or equal to the right operand.
a <= b
Example of the less than or equal operator:
a == b
Example of the equality operator:
a != b
Example of the inequality operator:
Boolean is the result
A comparison returns a value like all other operators. The value, in this case, is a boolean.
- true is “yes”, “correct” or “the truth”;
- false is “no”, “wrong” or “not the truth”.
A comparison result can be assigned to a variable, like any value:
Example of the string comparison:
In the examples the comparison 'Z' > 'A' gets to a result at the first step while the strings "Want" and "Walk" are compared character-by-character:
- W is the same as W.
- a is the same as a.
- n is greater than l.
Comparison of different types
For boolean values, true becomes 1, false becomes 0.
A regular equality check == has a problem, as it can’t differentiate 0 from false:
We meet the same thing with an empty string:
Comparison with null and undefined
Non-intuitive behavior is when null or undefined are compared to other values. For a strict equality check ===. These values are different, as each of them is a different type.
For a non-strict check ==
These two are a “sweet couple”, it means that they equal each other.
The Difference between == and ===
== (Double equals operator): the equality or abstract comparison operator
=== (Triple equals operator): the identity or strict comparison operator
Here are the differences between == and ===:
- before showing comparison == converts the variable values of the same type;
- === does not do any type conversion and returns true only if both values and types are identical for the two variables being compared.
For maths and other comparisons < > <= >=
Null/undefined are converted to numbers, here null becomes 0, undefined becomes NaN.
We present you some funny things that happen when we apply these rules. And also, how to not fall into a trap with them.
Strange result: null vs 0
Example of the compare null with a zero:
Mathematically, that’s strange. The last result says that "null is greater than or equal to zero", it means that in one of the comparisons above it must be true, but both of them are false.
The main reason is that an equality check == and comparisons > < >= <= work in a different way: comparisons convert null to a number, treating it as 0. That’s the reason why (3) null >= 0 is true and (1) null > 0 is false.
But on the other hand, the equality check == for undefined and null is defined without any conversions. They equal each other and don’t equal anything else, that’s why (2) null == 0 is false.
An incomparable undefined
We can’t compare the value undefined to other values:
The reasons why we get these results are:
- Comparisons (1) and (2) return false, as undefined gets converted to NaN, it’s a special numeric value which returns false for all comparisons.
- The equality check (3) returns false, as here undefined only equals null, undefined and no other value.