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
| Operator | Meaning | Example |
|---|---|---|
== | Loose equality; may convert types | 5 == "5" // true |
=== | Strict equality; compares value and type | 5 === "5" // false |
!= | Loose inequality | 5 != "5" // false |
!== | Strict inequality | 5 !== "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
| Operator | Meaning | Example |
|---|---|---|
< | left is less than right | 5 < 10 // true |
> | left is greater than right | 10 > 5 // true |
<= | left is less than or equal to right | 5 <= 5 // true |
>= | left is greater than or equal to right | 10 >= 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.