JavaScript comparison operators compare two values and return a Boolean result: true or false. The most important distinction is between strict equality (===, !==) and loose equality (==, !=), which performs type coercion.
Simple example:
const value = 5;
console.log(value === 5); // true
console.log(value === "5"); // false
console.log(value == "5"); // true because == coerces types
For new application code, strict equality is usually the safer default because it avoids many implicit-conversion surprises.
| Operator | Meaning | Example |
|---|---|---|
=== | Strict equal | 5 === 5 → true |
!== | Strict not equal | 5 !== "5" → true |
== | Loose equal with coercion | 5 == "5" → true |
!= | Loose not equal with coercion | 5 != "5" → false |
< | Less than | 4 < 5 → true |
> | Greater than | 6 > 5 → true |
<= | Less than or equal | 5 <= 5 → true |
>= | Greater than or equal | 5 >= 5 → true |
=== compares values without converting one operand to another type first.
console.log(5 === 5); // true
console.log(5 === "5"); // false
console.log(true === 1); // false
console.log(null === null); // true
This corrects a contradictory comment in the old example where 5 == "5" was described as false even though loose equality makes it true.
== performs coercion according to JavaScript's abstract equality rules. That can be intentional in specialized code, but it is easier to misread.
console.log(5 == "5"); // true
console.log(false == 0); // true
console.log("" == 0); // true
console.log(null == undefined); // true
=== and !==. If you deliberately use loose equality, do so because you understand the exact coercion behavior required.const value = 5;
console.log(value !== "5"); // true
console.log(value != "5"); // false
console.log(value !== 4); // true
The relational operators compare ordering. They are common in loops, ranges, validation, and sorting logic.
const score = 75;
console.log(score > 50); // true
console.log(score >= 75); // true
console.log(score < 100); // true
console.log(score <= 74); // false
Mixed-type relational comparisons can also trigger conversion, so normalize input types before comparing user-entered numeric data.
Strings are compared lexicographically using Unicode code-unit ordering, which is case-sensitive and is not the same as locale-aware human sorting.
console.log("apple" < "banana"); // true
console.log("Z" < "a"); // true in code-unit ordering
console.log("10" < "2"); // true as strings
For user-facing language sorting, consider localeCompare() or Intl.Collator instead of assuming < gives natural-language order.
NaN is unusual because it is not equal to itself under either === or ==. Use Number.isNaN() when you need to detect it.
const result = Number("not a number");
console.log(result === NaN); // false
console.log(Number.isNaN(result)); // true
Objects and arrays are compared by reference identity, not by recursively comparing their contents.
const a = { score: 10 };
const b = { score: 10 };
const c = a;
console.log(a === b); // false
console.log(a === c); // true
If you need structural equality, compare the relevant fields or use a deliberately chosen deep-comparison strategy.
Object.is() is another identity comparison operation. It differs from === mainly for NaN and signed zero.
console.log(Object.is(NaN, NaN)); // true
console.log(NaN === NaN); // false
console.log(Object.is(0, -0)); // false
console.log(0 === -0); // true
JavaScript evaluates a < b < c from left to right. The first comparison becomes a Boolean and that Boolean is then compared with c, which is rarely what was intended.
const x = 15;
const inRange = 10 < x && x < 20;
console.log(inRange); // true
const age = 20;
if (age >= 18) {
console.log("Adult");
} else {
console.log("Minor");
}
Combine comparisons with logical operators when more than one condition must be tested, and see if...else for conditional execution.
=) when equality (===) was intended.== and === behave the same.NaN with === NaN.0 < x < 10.Continue with logical operators, if...else, typeof, and variables.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.