</>
Skip to content
JavaScript lessons (30/64)

JavaScript — Comparisons

Every comparison returns a boolean

5 > 3        // true
2 === 1      // false
"b" >= "a"   // true

Feed results straight into if or store them:

const adult = age >= 18;

Equality: the two flavors

// STRICT (always use)
5 === 5       // true
5 === "5"     // false — different types, no conversion

// LOOSE (avoid writing; must recognize)
5 == "5"      // true  😱 — coerces "5" to number first
0 == ""       // true
null == undefined   // true

Loose equality's conversion table is famously unintuitive. Modern rule: === / !== always. ESLint's eqeqeq rule enforces it in real teams.

The full relational set

OperatorQuestion
=== / !==identical? / not identical?
< / >less / greater
<= / >=inclusive bounds

String comparison — alphabetical-ish

"apple" < "banana"    // true — dictionary order
"Zebra" < "apple"     // true 😐 — UPPERCASE sorts before lowercase
"10" < "9"            // true as strings! ("1" < "9")

Gotchas: case sensitivity and string-vs-number confusion. For real sorting use localeCompare or numeric sort callbacks (arrays lesson).

Tricky corners worth knowing

NaN === NaN          // false! NaN equals nothing, not even itself
Number.isNaN(x)      // correct NaN test

null === undefined   // false (different types)
null == undefined    // true (loose quirk)

+0 === -0            // true

Chaining comparisons doesn't chain

18 <= age <= 65     // BUG — evaluates (18<=age) → boolean → boolean<=65
age >= 18 && age <= 65   // ✅ the correct range check

Math-class notation silently lies. Combine with &&.

Comparing objects & arrays

References, never contents:

[1,2] === [1,2]              // false — two different arrays!
{a:1} === {a:1}              // false

const a = [1,2];
const b = a;
a === b                      // true — same array, two names

Content-equality needs a helper (JSON.stringify(a) === JSON.stringify(b) for simple cases) or a library deep-equal.

Mini Practice

  1. Predict-then-run five == vs === pairs from the lesson
  2. Build a valid-range checker for percentage input (0–100 inclusive)
  3. Show "10" < "9" and explain in one comment why
  4. Prove NaN !== NaN; fix with Number.isNaN
  5. Compare two arrays by contents via JSON.stringify; note when it lies (key order)

Next: booleans →

Related Topics

Frequently Asked Questions about Comparisons

What is Comparisons in JavaScript?

Comparisons is a fundamental concept in JavaScript. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Comparisons?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Comparisons.

Why is Comparisons important in JavaScript?

Comparisons is essential for JavaScript development. Understanding this concept will help you write better code and solve real-world problems more effectively.