JS 101 (4 - n) Comparisons
Tags: javascript, beginners, codenewbie, webdev
Published: 2021-01-13 17:58:07

**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.

<div class="table-wrapper-paragraph"><table>
<thead>
<tr>
<th><center> Content </center></th>
</tr>
</thead>
<tbody>
<tr>
<td><center><a href="https://dev.to/vplentinax/js-101-i-n-fundamentals-3cg5"><strong>JS 101 (1-n)</strong> - Introduction</a></center></td>
</tr>
<tr>
<td><center><a href="https://dev.to/vplentinax/js-101-2-n-fundamentals-25l3"><strong>JS 101 (2-n)</strong> - Data types</a></center></td>
</tr>
<tr>
<td><center><a href="https://dev.to/vplentinax/js-101-3-n-fundamentals-553o"><strong>JS 101 (3-n)</strong> - Type conversion </a></center></td>
</tr>
</tbody>
</table></div>

## **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**.

<img src="https://i.ibb.co/HqnRHbY/stringss.png" alt="stringss" border="0">

We are going to see the algorithm that allows us to solve these operations.

<img src="https://i.ibb.co/vBKQHdb/stringex.png" alt="stringex" border="0">

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)*

<img src="https://i.ibb.co/BKPjSH6/string3.png" alt="string3" border="0">

### **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](https://dev.to/vplentinax/js-101-3-n-fundamentals-553o)


<img src="https://i.ibb.co/0fVgMCq/string5.png" alt="string5" border="0">

<p>&nbsp;<p>
  > ⚠️ **`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**

<img src="https://i.ibb.co/BtqwnDH/strig6.png" alt="strig6" border="0">

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]( https://dev.to/vplentinax/js-101-2-n-fundamentals-25l3) 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!


If you want to read more about Javascript:
- [Java vs Javascript] (https://dev.to/vplentinax/java-vs-javascript-4hbd)
- [Javascript behind scenes](https://dev.to/vplentinax/javascript-behind-scenes-1d9)

If you want to read about other topics:
- [Classification of computer languages ​​(I)](https://dev.to/vplentinax/computer-languages-i-24pb)
- [Software development models and methodologies (Part 1)] (https://dev.to/vplentinax/software-development-models-and-methodologies-part-1-1kl5)
- [Algorithm](https://dev.to/vplentinax/algorithm-1ao0)
- [Suggestion for developing web pages with HTML](https://dev.to/vplentinax/suggestion-for-developing-web-pages-with-html-3fam)


Follow me!
[Instagram](https://www.instagram.com/vplentinax/)
[Twitter](https://twitter.com/vplentinax)