Basic comparison operators every beginner dev should know

Basic comparison operators every beginner dev should know

ยท

5 min read

JavaScript is a programming language that requires so many principles and is so confusing that any incorrect syntax can result in an error. You'll be so frustrated that you won't be able to pinpoint the source of the problem. It could be as simple as mixing up the following operators: <, >, <=, >= , ==, ===, != and !==.

You should be aware that each programming language has its own syntax, which can lead to confusion with these basic and common operators. Each operator has a unique meaning and is used in different contexts.

Prerequisite

To follow along with this article:

  1. Have a basic understanding of HTML5 and CSS3

  2. Download any IDE of your choice. I prefer Visual Studio Code

  3. Have a browser. I recommend Google chrome

What is a comparison operator?

Comparison operators are used to compare expressions. They are small pieces of logic that tell us whether or not a condition in our program is met. A comparison operator always returns a Boolean expression, i.e., true or false.

We have 3 types of comparison operators.

  1. Relational operator

  2. Abstract or loose operator

  3. Strict operator

Relational Operator

These operators compare values with one another. e.g. greater than(>), less than(<), greater than or equal to(>=), and less than or equal to (<=)

Greater than (>) sign

The greater than (>) sign compares the values of two expressions. When comparing the two values, it looks for the larger value and returns that value as true. If it is less, false is returned.

Example showing greater than(>)sign.

greater than (_) sign.png

From the above example, the bigger number was evaluated first, so it returned true.

Less than sign(<)

The less than ( < ) sign compares two expressions' values. The less than sign returns true, if the expression is less than the other expression; otherwise it will return false.

Example.

less than (_) sign (2).png The above example shows that 5 is greater than not less than 4 so it will return false.

Greater than or equal to(>=).

The greater than or equal to sign compares the values of two expressions. The greater than or equal one of the expressions must be met. If the expression is bigger than the other or equal to the other value. It will return true otherwise false if it is less or not equal to the other expression.

Example

greater than or equals to (_=) operator.png As I stated before, one of the conditions must be met, and the example above demonstrates that the number 20 is more than 19 but not equal to 19, making the expression to be evaluated as true.

In the next example, the equal sign is simply assigned to the greater than.

equals to (_=) .png The output is true because the number 20 is not greater but equal to the number compared with.

Example of a less than or equal to(<=) .

The less than or equal to (<=) sign compares the values of two expressions. If the expression on the left is less than or equal to the value on the right, the expression returns true; otherwise it will be returned as false.

less than or equals to (_=)operators .png

In the example above, the number 28 is compared with the number 29, which means that 28 is less than 29, and the output is true.

Abstract or loose operator

This operator checks to see if the expressions are the same.

We have the:

  1. Equality operator(==)

  2. Non-Equality operator(!=)

Equality operator

This operator is only concerned with the type of data stored for comparison and is unconcerned about the data type, this makes it a loose operator. This operator coaxes the data type, which is referred to as Type coercion in JavaScript. Making both the datatype and value return true.

Example

The equality operator.png

JavaScript interprets from left to right, so this has been converted to a string whereas the other expression was a number datatype.

Non-equality operator.

The non-equality operator is seen as a negative sign in JavaScript. It does not care about the data type.

Consider the following example

The Non-equality operator.png As previously stated, this operator is negative and does not stick to the datatype but instead converts the datatype to be the same as the other expression. As a result, the expression returned as false.

Strict Operator

These check to see if both expressions are of the same type.

We have:

  1. Strict equality(===)

  2. Strict Non-equality operator(!==)

Strict equality operator

This operator (===) checks both the data type and the value are the same; no type coercion is performed.

Example of a strict equality operator

Strict equality (===) operator.png

Strict Non-equality operator

The strict inequality operator ( !== ) checks whether its two expressions are not equal.

An example of a strict non-equality operator

Strict Non-equality (!==) operator.png

Remember the !== compares the expression that is not equal. In the above example, both the datatype and the value are the same when compared. Therefore, the expression will output a false.

To get a true both expressions will be different.

Example

Strict non-equality(!==) operator.png

Conclusion

  • Comparison operators are used to compare values in your program. They must return Boolean.

  • Comparison operators are categorized into 3 main types: Relational Operators, Abstract or loose Operators, and Strict Operators.

  • The strict equality operator (===) is superior to the equality operator (==). The strict equality operator (===) is best used when you don't want any bugs in your code.

Note

The assignment operator (=) this operator is quite different from the comparison operator. It is used to simply assign a value to a variable.

example

The assignment operator.png

THANKS FOR READING!!!

If you enjoyed reading this article, leave a ๐Ÿ’œ and share it with your friends.

Did you find this article valuable?

Support Cent Blog </๐Ÿ’–> by becoming a sponsor. Any amount is appreciated!

ย