Last updated: Mar 1, 2024
Reading timeยท4 min
To make the String.includes()
method case insensitive, convert both of the
strings in the comparison to lowercase.
A case-insensitive comparison is done by converting the two strings to the same case.
const str = 'HELLO WORLD'; const substr = 'hELLo'; // ๐๏ธ true console.log(str.toLowerCase().includes(substr.toLowerCase())); if (str.toLowerCase().includes(substr.toLowerCase())) { // ๐๏ธ this runs console.log('The substring is contained in the string'); } else { console.log('The substring is NOT contained in the string'); }
We converted both strings to lowercase to perform a case-insensitive lookup using the String.includes method.
We could achieve the same result by converting the strings to uppercase.
const str = 'HELLO WORLD'; const substr = 'hELLo'; // ๐๏ธ true console.log(str.toUpperCase().includes(substr.toUpperCase())); if (str.toUpperCase().includes(substr.toUpperCase())) { // ๐๏ธ this runs console.log('The substring is contained in the string'); } else { console.log('The substring is NOT contained in the string'); }
We just need the strings to be in the same case to be able to determine if the substring is contained in the string.
To do a case insensitive check if an array contains a string:
Array.findIndex()
method to iterate over the array.const names = ['John', 'Bob', 'Adam']; const name = 'ADAM'; const index = names.findIndex(element => { return element.toLowerCase() === name.toLowerCase(); }); console.log(index); // ๐๏ธ 2 if (index !== -1) { // ๐๏ธ this runs console.log('The string is contained in the array'); } else { console.log('The string is NOT contained in the array'); }
The function we passed to the Array.findIndex method gets called with each element in the array until it returns a truthy value or iterates over the entire array.
Once the callback function returns a truthy value, the element's index
is
returned from findIndex()
.
The function in the example returned 2
because the 3rd element in the array
matched the string.
If the function we passed to findIndex
never returns a truthy value, then
findIndex
returns -1
.
const names = ['John', 'Bob', 'Adam']; const name = 'Tim'; const result = names.findIndex(element => { return element.toLowerCase() === name.toLowerCase(); }); console.log(result); // ๐๏ธ -1
Alternatively, you can use the Array.some()
method.
This is a three-step process:
Array.some()
method to iterate over the array.true
is returned.const names = ['John', 'Bob', 'Adam']; const name = 'JOHN'; const includesValue = names.some(element => { return element.toLowerCase() === name.toLowerCase(); }); console.log(includesValue); // ๐๏ธ true
The difference between Array.findIndex()
and Array.some()
is that some
returns true
or false
, instead of the index of the array element or -1
.
The function we passed to the Array.some() method is called with each element of the array until a truthy value is returned or the array's values are exhausted.
An alternative would be to convert both strings to uppercase to achieve the same result.
Alternatively, you can use the Array.find()
method.
This is a three-step process:
Array.find()
method to iterate over the array.const names = ['John', 'Bob', 'Adam']; const name = 'ADAM'; const result = names.find(element => { return element.toLowerCase() === name.toLowerCase(); }); console.log(result); // ๐๏ธ 'Adam'
The function we passed to the Array.find method gets called with each element in the array until it returns a truthy value or iterates over the entire array.
If the callback function returns a truthy value, the corresponding element is
returned from the Array.find()
method.
If all invocations of the callback function return a falsy value, the
Array.find()
method returns undefined
.
If you need to perform a case-insensitive check whether a string is contained in an array and get all matches:
Array.filter()
method to iterate over the array.Array.filter
method will return an array of all of the elements that
satisfy the conditionconst arr = ['HELLO', 'HeLlO', 'WORLD']; const str = 'HeLLo'; const matches = arr.filter(element => { return element.toLowerCase() === str.toLowerCase(); }); console.log(matches); // ๐๏ธ ['HELLO', 'HeLlO'] if (matches.length > 0) { // ๐๏ธ at least 1 match found in the array }
The function we passed to the Array.filter method gets called with each element of the array.
The Array.filter()
method does not stop iterating once the callback function
returns a truthy value. It returns an array of all elements that satisfy the
condition.
You can learn more about the related topics by checking out the following tutorials: