DEV Community

Valentiina VP

Posted on • Updated on

JS 101 (4 - n) Comparisons

Hello everyone
Today we will talk about comparison operations in javascript. Remember this is the 4th article related to the Javascript 101 Fundamentals series, you can find all the related articles in this table.

Comparisons

As in mathematics, in javascript there are comparison operations that allow us to better manage the logic and flow of our algorithms to obtain the appropriate solution and develop our systems. These operations are represented as follows.

Name Symbol Example
Greater than `>` a > b
Greater or equals than `>=` a >= b
Less than `<` a < b
Less or equals than `<=` a <= b
Equals to `==` 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.

String comparison

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)

Comparison between different types

In these operations, Javascript automatically converts all operands to numbers. Remember how type conversions work by reading my previous post in this series: Type Conversion

⚠️ `Undefined` cannot be compared with another value because always will return `false`

Strict comparison

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.

A special situation

There are times that `==` handles peculiar Javascript data in a special way. This is the case of `null` and`undefined`. We said that `==` converts all data into numbers, if this were the case in these two types, `null` would be `0` and `undefined` 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 `true`.

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 `false`.

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!