javascript compare strings without being case sensitive

ankur picture ankur · Feb 7, 2011 · Viewed 192.2k times · Source

I have to check some strings using JavaScript but case sensitivity is causing problems. for example

if('abc'=='ABC')
{
return true;
}

it will not go inside the if loop though the meaning of the word are same. I cant use tolower clause too since i dont know the data how it would come it means for ex:

if('aBc'=='abC')
{
return true;
}

how to write the JS function for this if it could be done by jquery.

Answer

Gazler picture Gazler · Feb 7, 2011

You can make both arguments lower case, and that way you will always end up with a case insensitive search.

var string1 = "aBc";
var string2 = "AbC";

if (string1.toLowerCase() === string2.toLowerCase())
{
    #stuff
}