Javascript Compare The String With equalsIgnoreCase — Bondesk Blog

Raghuraman Kesavan
Bondesk.In
Published in
1 min readDec 28, 2019

In Javascript, We will perform string case insensitive comparison either by first using toUpperCase or by using toLowerCase method and then compare the strings.

let string1 = 'bondesk'; 
let string2 = 'BONDESK';
console.log(string1 === string2);
// false
console.log(string1.toUpperCase() === string2.toUpperCase());
// true
console.log(string1.toLowerCase() === string2.toLowerCase());
// true

Let’s write our own method which does string comparison without considering the case.

String.prototype.equalsIgnoreCase = function (compareString) { return this.toUpperCase() === compareString.toUpperCase(); 
};
const String1 = 'bondesk'; console.log(String1.equalsIgnoreCase("Bondesk")); //true console.log(String1.equalsIgnoreCase("BONDESK")); //true

The above code will perfectly work fine. If you are working on a large project where you need to perform a comparison of many places, then you can use the above method.

Even if you are performing any string manipulation throughout your application then you can generalize that by writing some prototype method and use it across.

Originally published at https://blog.bondesk.in on December 28, 2019.

--

--