How does larger and smaller Javascript string checking work?

Asked

Viewed 914 times

4

If you perform the condition '15000' > '100000' the result will be true

If you perform the condition 15000 > 100000 the result will be false

If you perform the condition '15000' > '200000' the result will be false

The question here is to understand the operation of javascript in the comparison of string and not know the right way to compare.

Can anyone explain?

2 answers

5

When there is a comparison '15000' > '100000', imagine that alphabetically the value 15000 is greater than 100000.

As if it were a classroom call list in which names are ordered alphabetically, this comparison would roughly be this way.

1º '10000'
2º '19520000'
3º '2'
...

Agree to the observation of @fernandosavio on the reference I made earlier, I suggest this site which has a more detailed explanation of string comparison.

  • 1

    "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.

  • 1

    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 :)

  • 1

    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 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

  • Good to know that there are many places to get knowledge and if you are offering knowledge, it is worth quoting :) Thanks guys

  • 2

    @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.

  • 1

    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

  • @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).

  • 1

    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

Show 4 more comments

2


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:

  1. c > cfalse, therefore, it is not possible to determine which character is larger.
  2. a > afalse, therefore, it is not possible to determine which character is larger.
  3. r > ctrue, 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.