Comparison Operators
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.
Syntax:
a > b
Example of the greater than operator:
Less than the operator (a < b) returns true if the left operand is less than the right operand.
Syntax:
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.
Syntax:
a >= b
Example of the greater than or equal operator:
Less than or equal operator (a <= b)< strong> returns true if the left operand is less than or equal to the right operand.
Syntax:
a <= b
Example of the less than or equal operator:
The equality operator (a == b) converts the operands if they are not of the same type, then applies strict comparison. If both operands are objects, JavaScript compares internal references which are equal when operands refer to the same object in memory.
Syntax:
a == b
Example of the equality operator:
If the operands are not equal, inequality operator (!=) returns true. When two operands are not of the same type, JavaScript tries to convert the operands to an appropriate type for the comparison.
If both operands are objects, JavaScript compares references that are not equal, when operands refer to different objects in memory.
Syntax:
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”.
String comparison
JavaScript uses a “dictionary” or “lexicographical” order to see if one string is greater than another. In other words, strings are compared to letter-by-letter.
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 comparing values of different types, JavaScript converts the values to numbers.
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.
Strict equality
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 ===
JavaScript has two visually similar, but very different ways to test equality:
== (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 of 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 to you some fun 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 > < >= <= < strong>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.
0 Comments
CAN FEEDBACK
Emoji