Understanding Comparison of Non-Numbers in JavaScript

Understanding Comparison of Non-Numbers in JavaScript

Understanding Comparison of Non-Numbers in JavaScript

When comparing non-numeric values, such as characters, JavaScript doesn't directly compare the letters themselves. Instead, it compares their associated unique values, known as Unicode values. These values are assigned to every character, ensuring consistent comparisons across all systems.


What Are Unicode Values?

Unicode is a standard that assigns a unique code point to every character, symbol, and even emoji. When you compare characters using comparison operators (>, <, >=, <=, etc.), JavaScript is actually comparing their Unicode values.

Example of Unicode Values:

  • Lowercase 'p': Unicode value 0070

  • Uppercase 'P': Unicode value 0050

Since 0070 > 0050, JavaScript will consider 'p' > 'P' as true.


Examples of Character Comparisons

Comparing Lowercase and Uppercase Letters

When comparing small a to capital A, the comparison is based on Unicode values:

javascriptCopyEditconsole.log('a' > 'A'); // true, because Unicode for 'a' is 97, and for 'A' is 65.

This behavior is consistent across all lowercase and uppercase letters.


Comparing Characters with Numeric Values

Let's look at a few more examples:

  1. Comparing digits:

     javascriptCopyEditconsole.log('5' > '4'); // true, as Unicode for '5' (53) is greater than '4' (52).
    
  2. Comparing lowercase letters:

     javascriptCopyEditconsole.log('b' > 'a'); // true, as Unicode for 'b' (98) is greater than 'a' (97).
    
  3. Comparing uppercase letters:

     javascriptCopyEditconsole.log('B' > 'A'); // true, as Unicode for 'B' (66) is greater than 'A' (65).
    
  4. Mixed comparisons:

     javascriptCopyEditconsole.log('z' > 'A'); // true, as Unicode for 'z' (122) is much greater than 'A' (65).
    


How Unicode Affects Sorting

When sorting an array of strings or characters, the sorting is based on Unicode values:

javascriptCopyEditlet letters = ['z', 'A', 'a', 'B'];
letters.sort();
console.log(letters); // ['A', 'B', 'a', 'z']

Here, uppercase letters appear bef

ore lowercase letters because of their lower Unicode values.


Key Takeaways

  1. Unicode Drives Comparisons: JavaScript uses Unicode values to compare characters, not their visual representation.

  2. Lowercase vs. Uppercase: Lowercase letters always have higher Unicode values than uppercase letters.

  3. Sorting: When sorting strings or characters, remember that sorting is Unicode-based. This can lead to results where uppercase letters come before lowercase ones.


Why Is This Important?

Understanding Unicode-based comparisons is crucial when working with:

  • Case-sensitive operations: Ensure you account for differences between lowercase and uppercase characters.

  • Sorting algorithms: Know that JavaScript sorts based on Unicode values.

  • Internationalization: Unicode enables consistent behavior across languages and scripts.