Greater Than and Less Than
You'll learn how to compare numbers using > and < operators to determine which value is larger or smaller.
What Are Comparison Operators?
The greater than (>) and less than (<) operators let you compare two values to see which is bigger or smaller. They always return a boolean: true or false.
Think of them like a seesaw: if the left side is heavier (greater), > tips to true. If the right side is heavier, < tips to true.
Comparing Numbers
5 > 3 // true (5 is greater than 3)
5 < 3 // false (5 is NOT less than 3)
10 > 10 // false (10 is not greater than itself)
-5 < 0 // true (negative numbers are smaller)
These operators work exactly as you'd expect with regular numbers.
What About Strings?
Strings compare lexicographically (like dictionary order), based on Unicode values:
"b" > "a" // true ("b" comes after "a")
"apple" < "banana" // true (alphabetical order)
"10" < "9" // true (compares as strings: "1" comes before "9")
Watch out: "10" < "9" is true because string comparison happens character-by-character, not numerically!
Mixing Types
When comparing different types, JavaScript attempts type coercion:
5 > "3" // true (string "3" converts to number 3)
true > 0 // true (true converts to 1)
null < 1 // true (null converts to 0)
However, comparisons with undefined or NaN always return false.
Key Takeaway: Use > and < to compare numbers; they return booleans and work reliably with numbers, but be cautious with strings and mixed types where conversion rules apply.