Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js

Making Comparisons

Comparing two different values is part of real everyday life. We compare prices from different stores, and then we decided which price is closest to our interest to chose. That means, that even when this happens in milliseconds, we compare things, and based on the outcome of these things we decide if we will do something or not.

This concept doesn't change much to programming and in our case we will see how to make comparisons by using JavaScript comparison operators.

 

Comparison operators allow us to compare two different parts. This could vary and there are many types of operators that we can pick. From checking equality, inequality, if something is greater or lower than it's counterpart and many more. All JS comparison operators are:

> Checking if the expression on the left is bigger than the one on the right

Checking if the expression on the left is smaller than the one on the right

>= Checking if the expression on the left is bigger or equal than the one on the right

<= Checking if the expression on the left is less than or equal than the one on the right

!= Checking if the expression on the left is not equal to the one on the right

== Checking if the expression on the left is equal than the one on the right

=== Checking if the expression on the left is equal than the one on the right. Checks type also.

The most important thing to understand and keep from this is that the data type that is returned from each and one of those operations is a boolean! That means that we inject the whole comparison operation inside an if statement's parentheses. Since whatever is inside the parentheses evaluates to true or to false, we will execute the following block of code conditionally.