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:
7 > 4 // true
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:
4 < 7 // true
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:
7 >= 4 // true 4 >= 4 // true
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:
4 <= 7 // true 4 <= 4 // true
a == b
Example of the equality operator:
1 == 1 // true "2" == 2 // true 3 == '3' // true 0 == false // true 0 == null // false 0 == undefined // false null == undefined // true
a != b
Example of the inequality operator:
1 != 2 ; // true 2 != "2"; // false 3 != '3' ; // false 1 != true; // false 0 != false; // false
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”.
console.log( 3 > 1 ); // true (correct) console.log( 3 == 1 ); // false (wrong) console.log( 3 != 1 ); // true (correct)
A comparison result can be assigned to a variable, like any value:
let result = 7 > 4; // the result of the comparison console.log ( result ); // true
Example of the string comparison:
console.log( 'Z' > 'A' ); // true console.log( 'Want' > 'Walk' ); // true console.log( 'Too' > 'To' ); // true
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
console.log( '3' > 1 ); // true, string '3' becomes a number 3 console.log( '02' == 2 ); // true, string '02' becomes a number 2
For boolean values, true becomes 1, false becomes 0.
console.log( true == 1 ); // true console.log( false == 0 ); // true
A regular equality check == has a problem, as it can’t differentiate 0 from false:
console.log( 0 == false ); // true
We meet the same thing with an empty string:
console.log( '' == false ); // true
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.
console.log( null === undefined ); // false
For a non-strict check ==
These two are a “sweet couple”, it means that they equal each other.
console.log( null == undefined ); // true
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.
var val1 = 13; var val2 = 13; console.log(val1 == val2); // true console.log(val1 === val2); // also true
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:
console.log( null > 0 ); // (1) false console.log( null == 0 ); // (2) false console.log( null >= 0 ); // (3) true
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:
console.log( undefined > 0 ); // false (1) console.log( undefined < 0 ); // false (2) console.log( undefined == 0 ); // false (3)
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.