Comparison or Relational operators in JavaScript

Comparison Operators or Relational operators compares the values of the two operand. The comparison operators are less than <, less than or equal <=, greater than >, greater than equal >=, equal (== & ===) & not equal ( != & !==). All comparison operators return true or false.

Equality Operators == & ===

There are two operators for checking equality in JavaScript. One is (==) known as an equality operator or loose equality operator. The other one is (===) strict Equality operator.

Equality Operators in JavaScript

Not Equal Operators != & !==

!= operator checks the un equality of two operands. It is similar to == except it returns true if operands are not equal. The !== operator is similar to === except it returns true if operands are not equal.

Not Equal Operators in JavaScript

Less than (<)

The Less than (<) comparison operator checks if the left operand is less than its right operand. If yes then it returns true otherwise returns false

Examples:

Greater than (>)

The greater than > operator checks if the value of the left operand is greater than the right operator. If yes then it returns true else false.

Less than or equal (<=)

The less than or equal (<=) operator returns true if the left operand is less than or equal to its right operand. else it evaluates to false.

Greater than or equal (>=)

The Greater than or equal (>=) relational operator returns true if its left operand is greater than or equal to its right operand else it returns false.

How Comparison works

The comparison is done only using the number comparison or string comparison. If the operands are of different types, then they are either converted to numbers or strings before they are compared

For Example

If both operands are numbers, then use the number comparison.

if both operands are string, then uses the string comparison

If one of the operands is a number, the other operand is converted to a number and then uses the number comparison. If the other operand does not convert to a number then the result is always false

If the operands are neither string nor number, and if they do convert to number or string, then the comparison operator converts them and compares them. The Example is a Date that converts to a number.

Note that using Equality Operators == & === does not work on dates. Because the Date is an object and to be considered equal, they must point to the same object.

Comparison involving any other types will always result in false.

Comparison Operators and Strings

The strings comparison uses the dictionary or lexicographical order. And the comparison is done character-by-character basis, using the Unicode values of the characters.

A character-by-character comparison starts with comparing the first character of both operands. If these are greater or less, then the comparison ends and the result is returned.

If the characters are equal, then the comparison moves to the next character. And the process continues until it reaches the end.

In the end, if both operands are finished, then the strings are Equal. else the operand with a longer length is considered greater.

The string comparison is case-sensitive. All capital letters are “less than” all lowercase letters. For a case insensitive comparison, you need to convert the string either to upper case or lower case.

Comparison Operators and Date

As you can see, the comparison operators on date work correctly. But Equality operators fail. An interesting thing to note here is that the >= & <= operators also check for Equality.

The difference is due to the fact how javascript handles these comparisons. The Equality Operators in JavaScript check if the objects are of the same instance

The comparison operators do not check for equality, when we use >= & <= operators.

The greater than or equal (date1 >= date2) internally uses the not greater than , (!(date1 < date2)) Hence avoiding the use of Equality Operators.

Similarly, the less than or equal uses the not less than internally as shown below.

Reference

  1. Expressions & Operators
  2. Precedence & Associativity

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top