Comparison operators are used to compare two variables. These variables can be numbers, strings, booleans depending on the operator used. The comparison operators return true or false based on the operands used. Comparison operators are mostly used expressions to determine equality or difference between two operands. Below is a list of all the comparison operators:
For equality comparison, the use of
!== is preferred.
=== checks if the two operands are of equal value and of equal type, while
== just checks if they are of equal value.
A few comparison operators are shown above with comments on how they function. The toughest concept is the difference between
===. As stated in the paragraph above, the
=== operator checks if two operands have the same value AND are of the same data type, while the
== operator checks if two operands hold the same value. This is why
c == d prints true even though
c is a number while
d is an object. Therefore,
c === d will print false even though they both hold the value 2.