How to make includes() case insensitive in Javascript

Javascript includes case insensitive example tutorial we learn how to make includes() case insensitive in JavaScript;

To make string to case insensitive in JavaScript we can use String.includes() with convert the strings to lowercase.

// not supported in IE 6-11
const str = 'CODING DRIVER';
const substr = 'coDiNg';

console.log(str.toLowerCase().includes(substr.toLowerCase())); //true

if (str.toLowerCase().includes(substr.toLowerCase())) {
  // the substring is included in the string
}

Using the Array.filter methods you can perform the case insensitive check whether a string is contained in an array and get all matches:

The Array.filter method will return an array of all of the elements that are matched the condition, You just need to convert the array element and the string to lowercase and do an equality check.

const arr = ['CODING', 'coDinG', 'DRIVER'];
const str = 'cOdIng';
const matches = arr.filter(element => {
  return element.toLowerCase() === str.toLowerCase();
});

console.log(matches); // ['CODING', 'coDinG']

if (matches.length > 0) {
  // at least 1 match found in array
}

To perform a case insensitive check whether a string is contained in an array:

The Array.find method will return an array of all of the elements that are matched the condition, You just need to convert the array element and the string to lowercase and do an equality check.

const arr = ['CODING', 'DRIVER'];
const str = 'CodINg';
const found = arr.find(element => {
  return element.toLowerCase() === str.toLowerCase();
});

console.log(found); // CODING

if (found !== undefined) {
  // string is in array
}

I hope these examples help you.