Ignoring case Check if Array contains String - JavaScript

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

Ignoring case Check if Array contains String - Array.findIndex #

To do a case insensitive check if an array contains a string:

  1. Use the Array.findIndex() method to iterate over the array.
  2. Convert each array element and the string to lowercase and check for equality.
  3. If the condition is met, the element's index is returned.
index.js
const names = ['John', 'Bob', 'Adam']; const name = 'ADAM'; const index = names.findIndex(element => { return element.toLowerCase() === name.toLowerCase(); }); console.log(index); // 👉️ 2

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.

By converting the array element and the string to lowercase, we are able to do a case-insensitive comparison.

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.

index.js
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.

Ignoring Case Check if Array contains String - Array.some #

To do a case insensitive check if an array contains a string:

  1. Use the Array.some() method to iterate over the array.
  2. Convert each the array element and the string to lowercase and check for equality.
  3. If the condition is met, true is returned.
index.js
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.

By converting both the array element and the string to lowercase, we are able to do a case-insensitive equality check.

An alternative would be to convert both strings to uppercase to achieve the same result.

Alternatively, you can use the Array.find method.

Ignoring Case Check if Array contains String - Array.find #

To do a case insensitive check if an array contains a string:

  1. Use the Array.find() method to iterate over the array.
  2. Convert each the array element and the string to lowercase and check for equality.
  3. If the condition is met, the matching element is returned.
index.js
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.

We do a case-insensitive comparison by converting both the array element and the string we check for to lowercase.

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.

Further Reading #

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.