3.4 Comparison Operators

The next category is comparison operators. As their name suggests, they compare the value of one variable with another. The result of any comparison expression is a boolean — either true or false. These booleans are exactly what conditional statements consume later to drive your application's logic.

Let's declare let x = 1; and see each operator in action:

console.log(x > 0);   // true
console.log(x >= 1);  // true
console.log(x < 1);   // false
console.log(x <= 1);  // true

These four are called relational operators. x < 1 returns false because x equals 1 — it is less than or equal to, not strictly less than.

Equality operators

JavaScript also provides two equality operators to test whether two values match. The recommended one is strict equality, written with three equal signs:

  • x === 1 returns true — both type and value match.
  • x !== 1 returns false — used to check inequality.

Always prefer === and !== over their loose counterparts == and !=; strict equality avoids confusing implicit type conversions. In the next video, we'll go deeper into equality operators.

Summary

This lesson introduces comparison operators used to evaluate the relationship between variables and return a boolean result (true or false). The lesson covers two main categories: relational operators (>, >=, <, <=) and equality operators (===, !==, ==, !=), demonstrating each with practical examples using the variable x = 1.

Key points

  • Comparison operators evaluate relationships between values and always return a boolean (true or false)
  • Relational operators (>, >=, <, <=) compare the magnitude of values; for example, x > 0 returns true when x equals 1
  • Equality operators (===, !==) check whether values are equal or not equal; for example, x === 1 returns true
  • The greater-than-or-equal-to (>=) and less-than-or-equal-to (<=) operators include equality in their comparison
  • Understanding comparison operators is essential for conditional logic and control flow in JavaScript

FAQ

What does a comparison operator return?

A comparison operator returns a boolean value: true if the condition is satisfied, false if it is not.

What is the difference between relational and equality operators?

Relational operators (>, >=, <, <=) compare the magnitude or size of values, while equality operators (===, !==) check whether values are exactly equal or not equal to each other.

Can you give an example of how the greater-than-or-equal-to operator works?

With x = 1, the expression x >= 1 returns true because x is equal to 1, satisfying the 'equal to' part of the operator.