# 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:

**Equality (==):**Checks if two operands are equal.**Strict equality (===):**Checks if two operands are equal in value and type.**Inequality (!=):**Checks if two operands are not equal.**Strict inequality (!==):**Checks if two operands are not equal in value or type.**Greater than (>):**Checks if the left operand is greater than the right operand.**Greater than or equal to (>=):**Checks if the left operand is greater than or equal to the right operand.**Less than (<):**Checks if the left operand is less than the right operand.**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.

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.

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.

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.

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.

## 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

**Nullish coalescing operator (??):**Provides a concise way to check for null or undefined values and provide a fallback value.**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.

**Related Topics**