JavaScript Comparisons
Comparison operators compare two values and return a boolean. They're the building blocks of every condition: "is the user old enough?", "is the cart empty?", "did the password match?".
| Operator | Meaning | Example | Result |
|---|---|---|---|
=== |
equal value and type | 5 === 5 |
true |
!== |
not equal (strict) | 5 !== "5" |
true |
== |
equal after type conversion | 5 == "5" |
true |
!= |
not equal (loose) | 5 != "5" |
false |
> / < |
greater / less than | 7 > 3 |
true |
>= / <= |
greater / less or equal | 3 <= 3 |
true |
Strict vs loose equality
=== checks that both sides have the same type and the same value — no conversions. == first tries to convert the two sides to a common type, following a set of rules that produces some odd results:
console.log(1 === "1", 1 == "1");
console.log(0 === false, 0 == false);
console.log("" == 0, "" === 0);
console.log(null == undefined, null === undefined);
console.log(null == 0, undefined == 0);
console.log([1] == 1, "1,2" == [1, 2]);false true
false true
true false
true false
false false
true true
Tip: Use
===and!==everywhere. The one widely accepted exception isvalue == null, a compact way to test for "eithernullorundefined".
Comparing numbers and strings
Numbers compare numerically. Strings compare character by character by Unicode code point — that's alphabetical for lowercase letters, but uppercase letters sort before lowercase, and digits are compared as characters:
console.log(10 > 9);
console.log("10" > "9"); // compares "1" with "9"
console.log("10" > 9); // mixed: the string becomes a number
console.log("apple" < "apricot");
console.log("Zoo" < "apple");
console.log("a".localeCompare("B", "en")); // locale-aware: a before Btrue
false
true
true
true
-1
When one side is a number and the other a string, <, >, <=, >= convert the string to a number. If it can't be converted (NaN), every comparison is false.
Special cases
console.log(NaN === NaN); // NaN equals nothing
console.log(Number.isNaN(NaN));
console.log(Object.is(NaN, NaN)); // Object.is treats NaN as equal
console.log(0 === -0, Object.is(0, -0));
console.log(null >= 0, null > 0); // relational ops convert null to 0
console.log(undefined > 0, undefined < 0, undefined == 0);false
true
true
true false
true false
false false false
Comparing objects
Objects (including arrays) are compared by reference: two variables are equal only if they point to the very same object. Equal-looking contents don't count:
const a = { x: 1 };
const b = { x: 1 };
const c = a;
console.log(a === b, a === c);
console.log([1, 2] === [1, 2]);
// Compare contents explicitly instead:
const sameArray = (p, q) => p.length === q.length && p.every((v, i) => v === q[i]);
console.log(sameArray([1, 2], [1, 2]));
console.log(JSON.stringify(a) === JSON.stringify(b)); // quick, for simple datafalse true
false
true
true
Using comparisons
Comparisons usually appear in if statements, ternaries and array methods:
const age = 16;
const status = age >= 18 ? "adult" : "minor";
console.log(status);
const temps = [12, 25, 31, 8];
console.log(temps.filter((t) => t > 20));
const between = (n, lo, hi) => n >= lo && n <= hi; // no chained comparisons!
console.log(between(5, 1, 10), 1 < 5 < 3); // 1 < 5 is true -> true < 3 -> 1 < 3minor
[ 25, 31 ]
true true
The last line is a trap: 1 < 5 < 3 doesn't mean "5 is between 1 and 3". It's evaluated as (1 < 5) < 3, which is true < 3, and true converts to 1. Always combine range checks with &&.
Test yourself
What is "2" > "12"?
- true
- false
- NaN
- TypeError
What is null == undefined?
- true
- false
- TypeError
- null
How should you check if x is between 1 and 10?
- 1 < x < 10
- x > 1 && x < 10
- x in [1, 10]
- x.between(1, 10)