1

I was taking a course that was talking about operators and during an exercise this example came up stating that 'a' > 'A' === true. It didn't explain why it is true. I've tried researching a bit on my own and thought maybe it has something to do with Lexicographical order, but that didn't seem right. I also checked to see if it had something to do with the location on the ASCII and Unicode alphabet, but that doesn't seem to be the case either. Can someone explain why lowercase 'a' is greater than uppercase 'A' in JavaScript?

Moneer Kamal
  • 1,837
  • 16
  • 25
bgongre
  • 57
  • 6
  • 3
    'a' == 97, 'A' = 65 + [ECMAScript 11.8.5: The Abstract Relational Comparison Algorithm](https://www.ecma-international.org/ecma-262/5.1/#sec-11.8.5) – Andreas Jun 22 '18 at 14:21
  • [This may help you in understanding how ASCII ordering works](https://stackoverflow.com/questions/1136156/is-there-any-logic-behind-ascii-codes-ordering) – vsvk Jun 22 '18 at 14:28
  • Lexicographic ordering (in general and specifically of UTF-16 code units) is quite meaningless except that is an obvious [total ordering](https://en.wikipedia.org/wiki/Total_order). Perhaps the question should be, why do you care? Wouldn't [localeCompare](https://www.ecma-international.org/ecma-262/5.1/#sec-15.5.4.9) be more useful to you? – Tom Blodget Jun 22 '18 at 16:43
  • Keep in mind that references to ASCII are people trying to teach you something by relating it to what you might already know. JavaScript has nothing to do with ASCII; For strings, it uses the UTF-16 character encoding of the Unicode character set (just like Java, .NET, VB4/5/6, VBA, …). You probably already know this in a sense because of characters like . – Tom Blodget Jun 24 '18 at 00:00

4 Answers4

9

Because a IS > than A in ASCII and lower end UTF8

console.log("a > A is","a">"A","because a is","a".charCodeAt(0),"and A is","A".charCodeAt(0))

related: Is there any logic behind ASCII codes' ordering?

mplungjan
  • 169,008
  • 28
  • 173
  • 236
4

You can see the values of each char at the ascii table below

a = 97

A = 65

So 'a' is > than 'A'

enter image description here

Gerardo BLANCO
  • 5,590
  • 1
  • 16
  • 35
  • 1
    Thank you, I had the number ordering backwards. I thought that the lower the number the higher the placement in order, i.e. 65 being closer to 1 would make 'A' greater than 'a' whose number is 97. Thank you for your explanation. – bgongre Jun 22 '18 at 14:37
1

This is because a has the greater ASCII value than A:

console.log('a'.charCodeAt(0)) // 97
console.log('A'.charCodeAt(0)) // 65
console.log(97 > 65)           // true
Mamun
  • 66,969
  • 9
  • 47
  • 59
1

Most languages will do effectively the same thing:

For example PowerShell

[char]'a' -gt [char]'A'

we can type-cast the letters to char (not string) and the comparison will check their underlying Unicode value.

Why so many languages have this as a default behavior I'm less certain of.

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Austin T French
  • 5,022
  • 1
  • 22
  • 40