String Comparison
You'll learn how JavaScript compares strings character-by-character using Unicode values, and why "Banana" comes before "apple".
How String Comparison Works
When you compare two strings with operators like <, >, <=, or >=, JavaScript doesn't measure their length or count their letters. Instead, it compares them lexicographically — character by character, based on each character's Unicode code point value.
Think of it like a dictionary, but stricter. JavaScript starts at the first character of each string and compares their Unicode numbers. If they're different, the comparison is decided immediately. If they're the same, it moves to the next character, and so on.
The Unicode Order Matters
Here's the catch: uppercase letters have lower Unicode values than lowercase letters. Capital 'A' is 65, while lowercase 'a' is 97. This means uppercase letters come "before" lowercase in comparisons:
console.log("Apple" < "apple"); // true (A=65 < a=97)
console.log("Zebra" < "apple"); // true (Z=90 < a=97)
console.log("abc" < "abd"); // true (c=99 < d=100)
console.log("10" < "9"); // true (1=49 < 9=57)
Notice the last example: when comparing strings, "10" comes before "9" because it compares the characters "1" and "9", not the numeric values!
Practical Implications
This alphabetical-but-Unicode ordering means "Banana" really does come before "apple" because 'B' (66) is less than 'a' (97). If you want case-insensitive comparison, you'll need to normalize both strings first using toLowerCase() or toUpperCase().
Key Takeaway: String comparison uses lexicographic ordering based on Unicode code points, comparing character-by-character, which means uppercase letters always sort before lowercase ones.