A general requirement while working in javascript is case-insensitive string comparisons. Case-insensitive comparison means equating strings irrespective of their case. It does not matter if the string is in upper-case or lower-case. We need to equate them and check if they are the same. While there are multiple ways to perform this job, each technique has its benefits.


This article will help us learn how to do case-insensitive string comparisons in javascript using different ways and example illustrations.

Table of Contents:-

How to do case-insensitive comparison in javascript using toUpperCase() or ToLowerCase()

The toUpperCase() method in javascript returns the calling string converted to uppercase.

The toLowerCase() method in javascript returns the calling string converted to lowercase.

Example:-

Perform case-insensitive string comparison on “Hello Javascript: This is dummy string” and “JAVASCRIPT IS MOST POPULAR LANGUAGE”

Code using :-

let dummyString1 = "Javascript is most popular Language"
let dummyString2 = "JAVASCRIPT IS MOST POPULAR LANGUAGE"
console.log(dummyString1.toLowerCase() === dummyString2.toLowerCase())

OR

let dummyString1 = "Javascript is most popular Language"
let dummyString2 = "JAVASCRIPT IS MOST POPULAR LANGUAGE"
console.log(dummyString1.toUpperCase() === dummyString2.toUpperCase())

Explanation:-

Here, we use the toLowerCase() or toUpperCase() method in javascript to convert both the strings into one similar case, either upper or lower and then compare the strings to see if both the strings are equal or not. 

This is the easiest way to perform case-insensitive string comparisons in javascript.

Output:-

true

How to do case-insensitive comparison in javascript using RegEx

RegExp is the regular expression object. These objects are patterns used to match character combinations in strings.

Example:-

Perform case-insensitive string comparison on “Hello Javascript: This is dummy string” and “JAVASCRIPT IS MOST POPULAR LANGUAGE”

Code using :-

let dummyString1 = "Javascript is most popular Language";
let dummyString2 = "JAVASCRIPT IS MOST POPULAR LANGUAGE";
let pattern = new RegExp('^' + dummyString2 + '$', 'i');
if (pattern.test(dummyString1)) {
    console.log("String Matched");
}
else
{
    console.log("String Not Matched");
}

Explanation:-

Here, we use the test() method in javascript to execute a search and determine if the pattern matches the specified string. We have created an object of Regular expression to match a pattern similar to dummyString2. 

  • ^ specifies that the match must start at the beginning of the string.
  • $ specifies that the match must be at the end of the string.

Output:-

String Matched

How to do case-insensitive comparison in javascript using localeCompare()

The localeCompare() method in javascript returns a number that indicates the reference string’s sort order to the string calling the method. The localeCompare() method is used when we need to take care of internationalization as well in order to get accurate case-insensitive comparisons.

Example:-

Perform case-insensitive string comparison on “Hello Javascript: This is dummy string” and “JAVASCRIPT IS MOST POPULAR LANGUAGE”

Code using :-

let dummyString1 = "Javascript is most popular Language";
let dummyString2 = "JAVASCRIPT IS MOST POPULAR LANGUAGE";
function caseInsensitiveComparison(_string1, _string2, _locale) {
    return _string1.localeCompare(_string2, _locale, { sensitivity: 'base' }) === 0;
}
console.log(caseInsensitiveComparison(dummyString1, dummyString2, 'en'))
console.log(caseInsensitiveComparison(dummyString1, dummyString2, 'de'))

Explanation:-

The above code uses the localeCompare() method in javascript to compare the strings. In the above example, dummyString1 is the calling string, and dummyString2 is the reference string. One also needs to provide the specific locale. 

In the last two lines of the code, the same strings get compared but using different English and German locales.

Output:-

true
true

How to do case-insensitive comparison in javascript using toLocaleUpperCase()

The toLocaleUpperCase(locale) method in javascript returns the calling string value converted to upper case according to any locale-specific mappings. 

The javascript’s toLocaleLowerCase(locale) method returns the calling string value converted to lower case according to locale-specific mappings.

The locale is a parameter that specifies which locale should be used. Since this argument is optional, the locale is picked up from the host system if it is not present.

Example:-

Perform case-insensitive string comparison on “Hello Javascript: This is dummy string” and “JAVASCRIPT IS MOST POPULAR LANGUAGE”

Code using :-

let dummyString1 = "Javascript is most popular Language"
let dummyString2 = "JAVASCRIPT IS MOST POPULAR LANGUAGE"
console.log(dummyString1.toLocaleUpperCase('en-US') === dummyString2.toLocaleUpperCase('en-US'))

OR

let dummyString1 = "Javascript is most popular Language"
let dummyString2 = "JAVASCRIPT IS MOST POPULAR LANGUAGE"
console.log(dummyString1.toLocaleLowerCase('en-US') === dummyString2.toLocaleLowerCase('en-US'))

Explanation:-

The above code uses the toLocaleUpperCase() or toLocaleLowerCase() method in javascript to compare the strings in the example. We are passing ‘en-US’, which indicates that the English – United States locale should be used.

Output:-

true

Read More:

I hope this article helped you to understand how to perform case-insensitive comparisons in javascript strings. Good Luck!!