JavaScript

This page gently explains comparison operators: they compare two values and return a Boolean that can be used in a condition.

Goal: read comparison results and write conditions while keeping both the value and its type in mind.

Relational operators

Comparison operators answer questions such as “are these values equal?” or “is the left value smaller?”. The result is a Boolean: true or false.

Equality and inequality

OperatorMeaningExample
==Loose equality; may convert types5 == "5" // true
===Strict equality; compares value and type5 === "5" // false
!=Loose inequality5 != "5" // false
!==Strict inequality5 !== "5" // true

In new code, prefer === and !== unless you have a clear reason to use type conversion. They make the comparison rule easier to see.

Order comparisons

OperatorMeaningExample
<left is less than right5 < 10 // true
>left is greater than right10 > 5 // true
<=left is less than or equal to right5 <= 5 // true
>=left is greater than or equal to right10 >= 5 // true

Use a comparison in a condition

JavaScript

const age = 20;

if (age >= 18) {
  console.log("Adult");
}

The expression inside the parentheses becomes true or false, and the if statement uses that result to decide whether to run its block.

Related topics