Master TypeScript From Scratch

Learn TypeScript step by step with practical examples, interactive coding lessons, and real-world projects.

TypeScript Comparison Operators

Learn how to compare numbers, strings, and values using equality, strict equality, inequality, greater than, less than, and other comparison operators.

Simple Explanation: Comparison operators compare two values and return a boolean result: true or false. They are commonly used in conditions and decision-making.

What Will We Learn?

Comparison operators help us check whether values are equal, different, greater, or smaller.

  • Equal to using ==.
  • Strict equality using ===.
  • Not equal using !=.
  • Strict inequality using !==.
  • Greater than and less than.
  • Greater than or equal to and less than or equal to.

Comparison Operators Workflow

Follow these steps to understand how TypeScript comparisons work.

📥
Step 1
Input Values

Declare two values.

→
⚖️
Step 2
Compare

Apply a comparison operator.

→
✅
Step 3
Evaluate

Check the comparison result.

→
🖥️
Step 4
Get Boolean

Receive true or false.

1. Equal To Operator (==)

The equal to operator == compares two values for equality after applying JavaScript's type-conversion rules when necessary.

Example

let firstNumber: number = 10;
let secondNumber: number = 10;
let thirdNumber: number = 20;

console.log(firstNumber == secondNumber);
console.log(firstNumber == thirdNumber);

Expected Output

true
false

Both first and second values are 10, so their comparison returns true. Comparing 10 with 20 returns false.

2. Strict Equality Operator (===)

The strict equality operator === compares both value and type. It does not convert different types to make them equal.

Example

let firstValue: number = 10;
let secondValue: number = 10;

let thirdValue: any = "10";

console.log(firstValue === secondValue);
console.log(firstValue === thirdValue);

Expected Output

true
false

The first comparison returns true because both values are numbers equal to 10. The second returns false because one value is a number and the other is a string.

Best Practice: Prefer === for equality comparisons in most TypeScript applications because it avoids unexpected type coercion.

3. Not Equal Operator (!=)

The not equal operator != returns true when two values are unequal after applying JavaScript's type-conversion rules when necessary.

Example

let firstNumber: number = 10;
let secondNumber: number = 20;
let thirdNumber: number = 10;

console.log(firstNumber != secondNumber);
console.log(firstNumber != thirdNumber);

Expected Output

true
false

The first comparison returns true because 10 and 20 are different. The second returns false because both values are equal.

4. Strict Not Equal Operator (!==)

The strict not equal operator !== returns true if the values differ or if their types differ.

Example

let firstValue: any = 10;
let secondValue: any = "10";
let thirdValue: any = 20;

console.log(firstValue !== secondValue);
console.log(firstValue !== thirdValue);
console.log(firstValue !== 10);

Expected Output

true
true
false

The first comparison returns true because the types differ. The second returns true because the values differ. The last returns false because both values are the same number.

5. Greater Than Operator (>)

The greater than operator > checks whether the left value is greater than the right value.

Example

let studentMarks: number = 85;
let passingMarks: number = 35;

console.log(studentMarks > passingMarks);
console.log(passingMarks > studentMarks);

Expected Output

true
false

Since 85 is greater than 35, the first comparison returns true. The reverse comparison returns false.

6. Less Than Operator (<)

The less than operator < checks whether the left value is smaller than the right value.

Example

let courseFees: number = 25000;
let budget: number = 30000;

console.log(courseFees < budget);
console.log(budget < courseFees);

Expected Output

true
false

The course fees are lower than the budget, so the first result is true.

7. Greater Than or Equal To (>=)

The >= operator returns true when the left value is greater than or equal to the right value.

Example

let marks: number = 35;
let passingMarks: number = 35;

console.log(marks >= passingMarks);

marks = 60;

console.log(marks >= passingMarks);

Expected Output

true
true

A value equal to the minimum also passes the comparison. Both 35 >= 35 and 60 >= 35 return true.

8. Less Than or Equal To (<=)

The <= operator returns true when the left value is less than or equal to the right value.

Example

let availableSeats: number = 10;
let maximumSeats: number = 10;

console.log(availableSeats <= maximumSeats);

availableSeats = 12;

console.log(availableSeats <= maximumSeats);

Expected Output

true
false

Ten seats are equal to the maximum of ten, so the first result is true. Twelve seats exceed the maximum, so the second result is false.

9. Comparison Operators Summary Table

Operator Name Example Result
== Equal to 10 == 10 true
=== Strict equality 10 === 10 true
!= Not equal 10 != 20 true
!== Strict not equal 10 !== 20 true
> Greater than 20 > 10 true
< Less than 10 < 20 true
>= Greater than or equal to 10 >= 10 true
<= Less than or equal to 10 <= 10 true

Practical Example: CIIT Course Eligibility

Let's compare a student's marks with passing marks and check whether their course fees are within budget.

Example

let studentName: string = "Priya";
let marks: number = 75;
let passingMarks: number = 35;

let courseFees: number = 30000;
let studentBudget: number = 35000;

console.log("Student: " + studentName);
console.log("Passed: " + (marks >= passingMarks));
console.log("Fees Within Budget: " + (courseFees <= studentBudget));
console.log("Full Marks: " + (marks === 100));

Expected Output

Student: Priya
Passed: true
Fees Within Budget: true
Full Marks: false

The program uses comparison operators to evaluate eligibility and budget conditions.

Common Mistakes

  • Using == when strict equality is intended.
  • Confusing =, which assigns a value, with ===, which compares values and types.
  • Forgetting that comparison operators return boolean results.
  • Reversing the values in greater-than or less-than expressions.
  • Assuming that == and === always behave the same for different types.
Tip: Prefer === and !== for predictable equality comparisons in TypeScript.

Practice Task

Create a TypeScript program for checking a student's eligibility for course admission.

  • Student marks: 80
  • Minimum passing marks: 35
  • Course fees: 30000
  • Student budget: 25000
  • Required attendance: 75

Compare the marks with passing marks, course fees with the budget, and attendance with the required attendance.

Hint

let marks = 80;
let passingMarks = 35;

let courseFees = 30000;
let studentBudget = 25000;

let attendance = 80;
let requiredAttendance = 75;

console.log(marks >= passingMarks);
console.log(courseFees <= studentBudget);
console.log(attendance >= requiredAttendance);

Expected Output

true
false
true

Summary

In this lesson, we learned how to compare values using equality, strict equality, inequality, greater-than, less-than, and inclusive comparison operators.

Key Point: Comparison operators return true or false. Prefer strict equality operators when comparing values in TypeScript.