||a > b|
|Greater or equals than||
||a >= b|
||a < b|
|Less or equals than||
||a <= b|
||a == b|
|Equals to (use stric)||
||a === b|
|No equals to||
||a != b|
|No equals to (use stric)||
||a !== b|
The responses to these comparisons will always be Boolean: true or false. We are going to see some practical examples and understand some peculiarities of these operations.
The comparison between strings is done character by character. We must always keep in mind that this comparison is not based on the location of a letter in the alphabet, but is based on the UNICODE system.
We are going to see the algorithm that allows us to solve these operations.
As I said earlier, the comparison is done character by character. In these first two examples, we only had one character. But the algorithm changes a bit if it is more characters:
1. 1. We translate the first two characters of both strings to UNICOD
2. If they are the same, we continue with the following characters and return to step one. This repeats for all characters until one greater / less than the other is found, at which point, it stops.
3. The string with more characters will always be greater (how is the case in example 4)
Undefinedcannot be compared with another value because always will return
The difference between a simple(
!=) and a strict(
!==) comparison is that the simple comparison does not differentiate between data types as it converts all of them to numbers. The strict comparison does not do any conversion so it differentiates between different types of data.
There are times that
undefined. We said that
== converts all data into numbers, if this were the case in these two types,
null would be
NaN, which should be false when comparing. On this occasion,
== gives them a sense of equality more of meaning than of type, since both data have a sense of 'empty' or 'nothing', and in this case, if they are equal. Therefore, the result is
As we also said,
=== does not any conversion, and performs a comparison of both type and value, and in this case,
null is a
object, as we saw in the previous post Data types and
undefined is a
undefined type, therefore, when comparing them with the strict comparer the result is
For these reasons, it is recommended to use the strict version of the comparisons for greater precision, if necessary.
I hope you found this article interesting and useful. See you soon!
If you want to read about other topics:
- Classification of computer languages (I)
- Software development models and methodologies (Part 1)
- Suggestion for developing web pages with HTML