Check if Array of Strings contains a Substring in JS

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

Check if Array contains a Substring using find() #

To check if a JavaScript Array contains a substring:

  1. Call the Array.find method on the array.
  2. Use the includes() method to check if each element contains the substring.
  3. The Array.find method will return the value of the element that matches the substring.
index.js
const array = ['hello', 'world']; const substring = 'hell'; const match = array.find(element => { if (element.includes(substring)) { return true; } }); console.log(match); // 👉️ hello if (match !== undefined) { // array contains substring match }

The function we passed to the Array.find method gets invoked with each element of the array until it returns a truthy value or exhausts the array's elements.

If the function never returns a truthy value, then Array.find returns undefined.

We used the String.includes() method to check if the substring is contained in each array element.

Note that the String.includes method is case sensitive. For a case insensitive conditional check, convert both the substring and the array element to lowercase.

index.js
const array = ['hello', 'world']; const substring = 'HELL'; const match = array.find(element => { if (element.toLowerCase().includes(substring.toLowerCase())) { return true; } }); console.log(match); // 👉️ hello if (match !== undefined) { // array contains substring match }

Check if Array contains a Substring using findIndex() #

To check if a JavaScript Array contains a substring:

  1. Call the Array.findIndex method on the array.
  2. Use the includes() method to check if each element contains the substring.
  3. The Array.findIndex method will return the index of the array element that matches the substring.
index.js
const array = ['hello', 'world']; const substring = 'hell'; const index = array.findIndex(element => { if (element.includes(substring)) { return true; } }); console.log(index); // 👉️ 0 if (index !== -1) { // array contains substring match }

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.

If all of the calls of the callback function return falsy values, then findIndex returns -1.

You would use the findIndex method instead of find if you have to perform operations based on the element's index in the array, e.g. remove an element.

Check if Array contains a Substring using filter() #

To check if a JavaScript Array contains a substring:

  1. Call the Array.filter() method on the array.
  2. Check if each element contains the specific substring.
  3. The Array.filter() method returns an array with all the elements that satisfy the condition.
index.js
const array = ['hello', 'world']; const substring = 'hell'; const matches = array.filter(element => { if (element.includes(substring)) { return true; } }); console.log(matches); // 👉️ [ 'hello' ] if (matches.length > 0) { // array contains substring match }

The function we passed to the Array.filter method gets invoked with each element in the array regardless if the condition is satisfied.

This is useful in case you have multiple elements in the array that satisfy the condition and you need all of their values.

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.