How to do Case-Insensitive String comparison in JavaScript?

The simplest method for comparing strings without considering case in JavaScript is to convert both strings to either lowercase or uppercase using the toLowerCase() or toUpperCase() method.

Here are a few examples of how you might use the toLowerCase() and toUpperCase() methods to do case-insensitive string comparison in JavaScript:

Method 1 – toLowerCase()

let string1 = "yarkul.com";
let string2 = "YARKUL.COM";
if (string1.toLowerCase() === string2.toLowerCase()) {
  console.log("The strings are equal, ignoring case.");
} else {
  console.log("The strings are not equal.");
}

 

ignore case comparison toLowerCase() method

Method 2 – toUpperCase()

let string1 = "yarkul.com";
let string2 = "YARKUL.COM";
if (string1.toUpperCase() === string2.toUpperCase()) {
  console.log("The strings are equal, ignoring case.");
} else {
  console.log("The strings are not equal.");
}

 
ignore case comparison toUpperCase() method

Also, you may be interested to read my article on how to use the localeCompare() function to make case-insensitive comparing.

Leave a Comment