How to compare two strings with case sensitive in JavaScript

Viewed 36

Comparing two strings that are "Random" and "RandoM", would result in a true case. What I want to do is to check also for the lower and upper cases (case-sensitive) and return false in this example. Right now, what I have is

userText = userText + e.target.value;
            if (userText[userText.length-1] != typeracertext[userText.length-1])
            {
                e.target.value = e.target.value.substring(0, e.target.value.length-1);
                return;
            }

and it return true when there is difference in the lower-upper cases. Any ideas?

EDIT: Found a solution, so for anyone that is struggling to find one, here is your solution:

str1.localeCompare(str2) = 
//returns 0 if true
//return -1 if str2 is stronger
//returns 1 if str1 is stronger
1 Answers

I don't know if i'm understanding your question, but I hope it could help you:

a = "Random"; b = "RandoM";
a != b ==> true   (what you have now)
a.toLowerCase() != b.toLowerCase() ==> false (i believe that this is what you want).
You could also use .toUpperCase() of course

.

Regards,

Related