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:
Have a basic understanding of HTML5 and CSS3
Download any IDE of your choice. I prefer Visual Studio Code
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.
Relational operator
Abstract or loose operator
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.
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.
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
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.
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.
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:
Equality operator(==)
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
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
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:
Strict equality(===)
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 Non-equality operator
The strict inequality operator ( !== ) checks whether its two expressions are not equal.
An example of a strict non-equality operator
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
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
THANKS FOR READING!!!
If you enjoyed reading this article, leave a ๐ and share it with your friends.