Comparison
Comparison operators compare two values and return a boolean, either true or false. You use them in if statements, loops, filters and anywhere your code must make a decision, such as "is the user over 18?" or "has the cart got any items?".
The full set of comparison operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
=== | Strictly equal (same type and value) | 5 === 5 | true |
!== | Strictly not equal | 5 !== "5" | true |
== | Loosely equal (converts types first) | 5 == "5" | true |
!= | Loosely not equal | 0 != "" | false |
> < | Greater than, less than | 8 > 3 | true |
>= <= | Greater or equal, less or equal | 3 <= 3 | true |
Strict versus loose equality
The double-equals operator converts both sides to a common type before comparing. That produces results that are hard to predict: 0 == "" is true, "1" == 1 is true, and null == undefined is true, yet null == 0 is false.
Triple-equals never converts. If the types differ, the answer is simply false. Use === and !== by default. Many teams allow one exception, value == null, as a short way to check for both null and undefined at once.
const age = Number(prompt('Your age?'));
if (age >= 18) {
console.log('You can register.');
} else {
console.log('Sorry, adults only.');
}
console.log(5 === '5'); // false, different types
console.log(5 == '5'); // true, string converted to number
console.log('10' < '9'); // true, compared as text!
console.log(10 < 9); // false
Comparing strings
When both sides are strings, < and > compare character by character using their Unicode values. That is why "10" < "9" is true: the character "1" comes before "9". It is also why uppercase letters sort before lowercase ones, so "Zebra" < "apple" is true.
If you are comparing numbers that came from inputs, convert them first. For sorting words in a human-friendly way, use a.localeCompare(b), which handles case and language rules properly.
Traps with objects, arrays and NaN
- Objects compare by reference.
[1, 2] === [1, 2]is false, and so is{} === {}. Two separate objects are never equal, even with identical contents. Only two variables pointing to the very same object compare as equal. - NaN is not equal to itself.
NaN === NaNis false. UseNumber.isNaN(x)to test for it. - Chained comparisons do not work like maths.
1 < 5 < 3evaluates1 < 5totrue, then comparestrue < 3, which becomes1 < 3, so the result is true. Writea < b && b < cinstead. - Assignment instead of comparison.
if (x = 5)assigns 5 and is always truthy. Linters catch this, which is one good reason to use one.
Using results directly
Because a comparison already produces a boolean, you can store it: const isAdult = age >= 18;. Writing if (isAdult === true) is unnecessary; if (isAdult) says the same thing more clearly. Named boolean variables like this make conditions much easier to read.
To remember: prefer ===, convert inputs to numbers before using < or >, and never expect two different objects to be equal.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.