0

I have some old code that doesn't have any comments that is using javascript differently then how I've ever used it. The following is doing math on strings.

if ((toFind > nextOptionText) && (toFind < lookAheadOptionText))

I've found questions like this one - that basically states that "a" < "b":

how exactly do Javascript numeric comparison operators handle strings?

However in my example I have some special characters that it is comparing against. In my code the parameters from above are:

if (("A" > "+") && ("A" < ">EINFUHRUNG ZUM"))

This is equaling TRUE - For me in my case I need it to equal FALSE, but I'm not asking how to make it false, I really just want to understand what the developer that wrote this code was thinking and how does the above if statement work.

Obviously I'm also dealing with a foreign language (German) - I'm pretty sure that this code was written prior to the application becoming multi-lingual.
If there is other suggestions that I should look into, please let me know (i.e. like using the locale or doing a different type of comparison).

webdad3
  • 8,893
  • 30
  • 121
  • 223
  • The `if` statement in your edit would evaluate to `false`, not `true`. Can you post example code that shows the behavior you're seeing? – Rick Hitchcock Jan 04 '18 at 22:48

1 Answers1

2

My quick test shows that this code evaluates to FALSE, as expected.

if (("A" > "+") && ("A" < ">EINFUHRUNG ZUM")) {
    alert('true')
} else {
    alert( 'false') 
}

In general, the comparison is done as usual according to the character codes, therefore "A" > ">" and "A" > "+".

To compare strings with non-ASCII letters, you might find this reference useful.

texnic
  • 3,959
  • 4
  • 42
  • 75
  • I screwed up my original variables. There were out of order. I updated the question. – webdad3 Jan 04 '18 at 22:44
  • @webdad3, it doesn't change much: as long as the "standard" symbols (you have ">" in your string) are the first character, "A" (or any other letter) will be "greater" than the whole string. – texnic Jan 04 '18 at 23:46
  • your resources were very helpful in my understanding my issue. Thanks! – webdad3 Jan 05 '18 at 18:55