Course contentsShow
← JavaScript
Lesson 63 of 2,7232. Values and TypesFree lesson

Relational Operators: <, >, <=, >=

Understand how relational operators compare values and what coercion rules apply.

Relational Operators: <, >, <=, >=

What you'll learn: How JavaScript compares values to determine if one is less than, greater than, or equal to another.

The Core Idea

Relational operators let you compare two values to see their relationship in terms of magnitude or order. They always return a boolean: true or false.

  • < : less than
  • > : greater than
  • <= : less than or equal to
  • >= : greater than or equal to

Think of these like questions: "Is 5 less than 10?" → 5 < 10 → true

How They Work with Numbers

10 > 5      // true
3 < 3       // false
3 <= 3      // true
7 >= 10     // false

Straightforward! Numbers compare by their mathematical value.

String Comparisons

Strings compare lexicographically (dictionary order), character by character using Unicode values:

"apple" < "banana"  // true (a comes before b)
"Z" > "a"           // false (uppercase Z has lower Unicode value)
"10" < "2"          // true (string comparison: "1" comes before "2")

Type Coercion Alert

Like ==, relational operators perform type coercion when comparing different types:

"10" > 5     // true (string "10" coerced to number 10)
true > 0     // true (true coerced to 1)
null >= 0    // true (null coerced to 0)

This can create surprises! The string "10" becomes the number 10 before comparison.

Special Cases

NaN < 5      // false (any comparison with NaN is false)
NaN >= NaN   // false

Key Takeaway: Relational operators compare values using type coercion when types differ, returning boolean results—but watch out for unexpected string-to-number conversions and always-false NaN comparisons.