Javascript compares letter by letter, using the coding dictionary that JS uses (Unicode).
Hence, strings are not compared on a whole, but are divided and each character is compared individually, until one is larger or smaller than the other.
For example:
'A' > 'Z'
Would return false
, in view of the fact that, in the Unicode dictionary, the decimal code of the letters are:
'A'.charCodeAt(0) // 65
'Z'.charCodeAt(0) // 90
Soon, Z
is greater than A
.
Thus, when comparing strings carro
>
cachorro
, we have true
, since the comparison is made as follows:
c
>
c
⇒ false
, therefore, it is not possible to determine which character is larger.
a
>
a
⇒ false
, therefore, it is not possible to determine which character is larger.
r
>
c
⇒ true
, what ends the comparison, assessing that carro
is greater than cachorro
.
It’s like, underneath the scenes, Javascript does this:
const char1 = 'g'
const char2 = 'c'
console.log(
char1 > char2
)
console.log(
char1.charCodeAt(0) > char2.charCodeAt(0)
)
You can check the Unicode table here.
My response was immensely based in this section the excellent documentation of the Javascript website.info.
"The question here is to understand the operation of javascript in the comparison of String and not know the right way to compare". He wants to understand how to compare each type of data in Javascript.
– Giuliana Bezerra
I removed the line that I say is the correct way to compare. But the answer I wrote, reading it to the end, answers @felipema’s question where he wants to know only the comparison of numbers of string type. Thank you for the remark :)
– Victor Carnaval
Dude, the W3schools link doesn’t explain anything about string comparison, it just says "compare alphabetically" and that’s what you explain in your answer. I don’t know if this link needs to be there.. Not to mention that W3schools is not a good reference.
– fernandosavio
@fernandosavio bad reference is the MDN. I’ve seen terrible translations there and outdated information in droves. People speak ill of W3S but I’ve never seen anything wrong there rs. Everything I search there, the information is great. But, each one is each :D
– Sam
Good to know that there are many places to get knowledge and if you are offering knowledge, it is worth quoting :) Thanks guys
– Victor Carnaval
@Sam, bad reference is MDN? OK then. hahahah. There has already been a page called w3fools.com with several big names of web programming pointing out several errors of W3schools. But beauty.. I will only put a parenthesis, I always search the pages in English to avoid translation errors.
– fernandosavio
Well, it is Vitor. About W3schools, I think the following (my opinion): I always researched there a long time ago and always attended to me well (of course some things they do not delve much, but then you look for another source), until one day I heard someone say that W3scools was not good. Then I thought to myself: "Hi, it looks good to me"... so I think that w3Scools is not the 8th wonder of the world, and if you want to know in depth something, you look for official documentation etc, but I didn’t think that the W3S is bad just because I heard someone talk. Well, if someone proves me it’s bad I’ll find it’s bad
– Sam
@fernandosavio may have mistakes, all right, just as I’ve seen several on MDN. But I, particularly, until today do not remember seeing a mistake in W3S. And look I see many things almost every day there. And another, everything I see in one place, I look in another to compare, that is, everything I see in W3S I look in other sources and until today I saw no difference rs :D (at least not that I remember).
– Sam
If you access the W3fools he explains that this was a long time ago... It should no longer be the case anyway, but I lost confidence and now only left this tantrum.. hahahahaha
– fernandosavio