Ask If One Thing Equals Another
JavaScript Comparisons
Compare values with `===`, `!==`, `<`, `>`, `<=`, `>=`. Always use strict equality.
What you'll learn
- Use `===` and `!==` for equality
- Compare numbers and strings with `<`, `>`, `<=`, `>=`
- Avoid `==` and its silent type coercion
Comparison operators ask “how does this value compare to that one?”
They return a boolean — either true or false.
console.log(5 === 5); // true
console.log(5 !== 6); // true
console.log(5 > 3); // true
console.log(5 < 3); // false
console.log(5 >= 5); // true
console.log(5 <= 4); // false Always Use ===
You’ll see two equality operators in JavaScript code:
- “
===strict equality — compares value AND type” ==loose equality — converts types before comparing
Use ===. Always. The loose version has surprising results that
nobody remembers correctly.
console.log(5 === "5"); // false ← strict: different types
console.log(5 == "5"); // true ← loose: converts the string
console.log(0 == ""); // true ← surprising
console.log(0 == false); // true ← surprising
console.log(null == undefined); // true ← surprising Comparing Strings
Strings compare lexicographically — character by character, by their Unicode code points.
console.log("apple" < "banana"); // true
console.log("Apple" < "apple"); // true ← capitals sort first
console.log("10" < "9"); // true ← "1" < "9" as strings For human-friendly comparisons (alphabetical, case-insensitive,
locale-aware), use localeCompare:
console.log("Apple".localeCompare("apple"));
// 1 (or -1 in some locales) — handles case + accents per locale Comparing Different Types
Mixing types in comparisons with < / > triggers conversion to
numbers. It works, but the result is often a surprise.
console.log("5" < 10); // true ← "5" converted to 5
console.log("abc" < 10); // false ← "abc" becomes NaN
console.log("abc" > 10); // false ← NaN comparisons are all false Stay safe by comparing values of the same type.
NaN Doesn’t Compare With Itself
console.log(NaN === NaN); // false
console.log(NaN < 1); // false
console.log(NaN > 1); // false
console.log(Number.isNaN(NaN)); // true ← the correct check Up Next
Now that you can ask questions about values, time to act on the
answers. if/else.