Comparison or Relational operators in Typescript

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 Typescript. One is (==) known as an equality operator or loose equality operator. The other one is (===) strict Equality operator.

Equality Operators in Typescript

Not Equal Operators != & !==

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

Not Equal Operators in Typescript

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


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 number or string before they are compared

For Example

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

if the 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 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 Date which converts to a number.

Note that using Equality Operators == & === does not work on dates. Because date is 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.

In 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 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 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 that how javascript handles these comparisons. The Equality Operators in Typescript checks if the objects are of the same instance

The comparison operators does not checks 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.


  1. Expressions & Operators
  2. Precedence & Associativity

Read More

  1. Complete Typescript Tutorial
  2. Typescript Operators
  3. Arithmetic Operators
  4. Unary plus / Unary minus Operators
  5. Increment/Decrement Operators
  6. Comparison / Relational Operators
  7. Equality Operator / Strict Equality Operators
  8. Ternary Conditional Operators
  9. Logical Operators
  10. Bitwise Operators
  11. Assignment Operators
  12. Nullish coalescing operator
  13. Comma Operator in Typescript
  14. Operator Precedence

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