JavaScript compare strings ignore case | Example code

  • by

The simplest way to do compare strings ignore case is to call toUpperCase method in JavaScript. We can call it case insensitive string comparison.

Just use === operator

str1.toUpperCase() === str2.toUpperCase()

JavaScript compare strings ignore-case

Simple example code compare strings without being case sensitive.

<!DOCTYPE html>
<html>
<body>
  <script>
   var str1 = "aBc";
   var str2 = "AbC";

   if (str1.toUpperCase() === str2.toUpperCase())
   {
    console.log("Both string have same text")
  }
  
  </script>
</body>
</html> 

Output:

JavaScript compare strings ignore case

Or you can both string lower case.

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

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

Do comment if you have any doubts or suggestions on this JS string topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.