Documentation and Books

Recent site activity

Web Development‎ > ‎JavaScript‎ > ‎

How to compare two strings in a case insensitive way?

The following is a very nice solution to compare two strings in a case insensitive way, it uses the prototype functionality to add a method equalsIgnoreCase to the String class:

  <script type="text/javascript">

String.prototype.equalsIgnoreCase=equalsIgnoreCase;

function equalsIgnoreCase(arg) {
return (new String(this.toLowerCase())==(new String(arg)).toLowerCase());
}

</script>

You can use this new function in the following way:

  <script type="text/javascript">

var text = "HELLO";
alert(text.equalsIgnoreCase("hello"));

</script>