TypeScript Comparison Operators

TypeScript comparison operators are used to compare two values and evaluate their relationship. They return a boolean value (true or false) based on the comparison outcome. Comparison operators are essential for making decisions and controlling program flow in your TypeScript code.

List of Comparison Operators

Here are the different comparison operators available in TypeScript:

  1. Equality (==): Checks if two operands are equal.
  2. Strict equality (===): Checks if two operands are equal in value and type.
  3. Inequality (!=): Checks if two operands are not equal.
  4. Strict inequality (!==): Checks if two operands are not equal in value or type.
  5. Greater than (>): Checks if the left operand is greater than the right operand.
  6. Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right operand.
  7. Less than (<): Checks if the left operand is less than the right operand.
  8. Less than or equal to (<=): Checks if the left operand is less than or equal to the right operand.

Equality (==)

The equality operator (==) checks if two operands are equal, performing type coercion if needed. It converts operands to the same type before making the comparison.

let num1: number = 5; let num2: string = "5"; console.log(num1 == num2); // Result: true (coerced equality)

In this example, num1 and num2 have different types (number and string), but the equality operator coerces the types and returns true because their values are considered equal.

Strict Equality (===)

The strict equality operator (===) checks if two operands are equal in both value and type. It does not perform type coercion.

let num1: number = 5; let num2: string = "5"; console.log(num1 === num2); // Result: false (strict equality)

In contrast to the previous example, === returns false because it considers both the value and the type, and the types are different.

Inequality (!=)

The inequality operator (!=) checks if two operands are not equal. It performs type coercion if needed.

let str1: string = "hello"; let str2: number = 42; console.log(str1 != str2); // Result: true (coerced inequality)

Here, str1 and str2 have different types, but the inequality operator returns true after coercing the types.

Strict Inequality (!==)

The strict inequality operator (!==) checks if two operands are not equal in either value or type.

let str1: string = "hello"; let str2: number = 42; console.log(str1 !== str2); // Result: true (strict inequality)

In this case, !== returns true because it considers both the value and the type, and they are different.

Greater Than (>), Greater Than or Equal To (>=), Less Than (<), Less Than or Equal To (<=)

These operators compare numerical values to determine their relationship. They return true if the specified condition is met; otherwise, they return false.

let x: number = 10; let y: number = 5; console.log(x > y); // Result: true console.log(x >= y); // Result: true console.log(x < y); // Result: false console.log(x <= y); // Result: false

Type Safety

TypeScript comparison operators offer type safety by ensuring consistent type comparisons. They prevent comparing incompatible data types, avoiding unexpected results and runtime errors.

Operator Precedence

Comparison operators have a specific order of precedence within expressions. Understanding precedence helps avoid potential ambiguity and ensures accurate evaluation of your code.

Additional Features

  1. Nullish coalescing operator (??): Provides a concise way to check for null or undefined values and provide a fallback value.
  2. Conditional operator (?): Allows for conditional statement construction based on the comparison outcome.

Conclusion

TypeScript comparison operators, such as equality (==), strict equality (===), inequality (!=), strict inequality (!==), greater than (>), greater than or equal to (>=), less than (<), and less than or equal to (<=), are used to evaluate conditions and determine the relationship between values. The equality operators perform type coercion if needed, while the strict equality operators consider both value and type. These operators play a fundamental role in creating logical expressions and making decisions within TypeScript programs.