/ JAVASCRIPT, FRONT-END

How to make case insensetive in Javascript?

Question

There is an if statement as below.

if (str.indexOf("Abc") == -1) {
    console.log("pass");
}

I want to make Abc case insensitive, so Abc, abc, ABc etc will still pass through.

Answer

Add .toLowerCase() after str. This method will make all letters to lower case.

if (str.toLowerCase().indexOf("Abc") == -1) {
    console.log("pass");
}

In this way, Abc will convert into abc.

Reference


This is the end of post