Check If An Array Contains A Substring In JavaScript

To check if an array contains a substring in JavaScript, you can use any of the following methods –

  1. Use the Array.find() method to find the element that contains the substring in the array.
  2. Use the Array.findIndex() method to find the index of the element that contains the substring in the array.
  3. Use the Array.filter() method to return an array containing all the elements that match the substring.
  4. Use the Array.some() method to check if at least one element of the array match the substring.
  5. Use the Array.every() method to check if all the elements of the array match the substring.

Let’s discuss each of the above methods in detail.

Check If An Array Contains A Substring In JavaScript

check if an array contains a substring in javascript

Method 1: Use The Array.find() Method To Find The Element That Contains The Substring In The Array

The Array.find() method takes a callback function as its argument. This callback function can be used to check if an element of the array contains the substring. As soon as one of the elements containing the substring is found, it is immediately returned.

Let’s take an example to understand this method.

Suppose we have an array of strings as follows –

const arr = ["abc", "xyz", "pqr", "lmn"];

Now, let’s use the Array.find() method to check if this array contains the substring “xy”:

const result = arr.find(element => element.includes("xy"));

console.log(result); // "xyz"

In the above code, we have defined an array of strings – arr. We have used the Array.find() method to check if this array contains the substring “xy”. We have defined a callback function which checks if an element of the array contains the substring “xy”. If it does, then it is immediately returned.

In this case, the string “xyz” contains the substring “xy”. Hence, it is returned as the output.

If no element of the array contains the substring, then undefined is returned.

Let’s take an example to understand this.

Suppose we have an array of strings as follows –

const arr = ["abc", "pqr", "lmn"];

Now, let’s use the Array.find() method to check if this array contains the substring “xy”:

const result = arr.find(element => element.includes("xy"));

console.log(result); // undefined

In the above code, we have defined an array of strings – arr. We have used the Array.find() method to check if this array contains the substring “xy”. We have defined a callback function which checks if an element of the array contains the substring “xy”. If it does, then it is immediately returned.

In this case, no element of the array contains the substring “xy”. Hence, undefined is returned as the output.

Method 2: Use The Array.findIndex() Method To Find The Index Of The Element That Contains The Substring In The Array

The Array.findIndex() method takes a callback function as its argument. This callback function can be used to check if an element of the array contains the substring. As soon as one of the elements containing the substring is found, its index is immediately returned.

Let’s take an example to understand this method.

Suppose we have an array of strings as follows –

const arr = ["abc", "xyz", "pqr", "lmn"];

Now, let’s use the Array.findIndex() method to check if this array contains the substring “xy”:

const result = arr.findIndex(element => element.includes("xy"));

console.log(result); // 1

In the above code, we have defined an array of strings – arr. We have used the Array.findIndex() method to check if this array contains the substring “xy”. We have defined a callback function which checks if an element of the array contains the substring “xy”. If it does, then its index is immediately returned.

In this case, the string “xyz” contains the substring “xy”. Hence, its index 1 is returned as the output.

If no element of the array contains the substring, then -1 is returned.

Let’s take an example to understand this.

Suppose we have an array of strings as follows –

const arr = ["abc", "pqr", "lmn"];

Now, let’s use the Array.findIndex() method to check if this array contains the substring “xy”:

const result = arr.findIndex(element => element.includes("xy"));

console.log(result); // -1

In the above code, we have defined an array of strings – arr. We have used the Array.findIndex() method to check if this array contains the substring “xy”. We have defined a callback function which checks if an element of the array contains the substring “xy”. If it does, then its index is immediately returned.

In this case, no element of the array contains the substring “xy”. Hence, -1 is returned as the output.

Method 3: Use The Array.filter() Method To Return An Array Containing All The Elements That Match The Substring

The Array.filter() method takes a callback function as its argument. This callback function can be used to check if an element of the array contains the substring. All the elements containing the substring are returned in a new array.

Let’s take an example to understand this method.

Suppose we have an array of strings as follows –

const arr = ["abc", "xyz", "pqr", "lmn"];

Now, let’s use the Array.filter() method to check if this array contains the substring “xy”:

const result = arr.filter(element => element.includes("xy"));

console.log(result); // ["xyz"]

In the above code, we have defined an array of strings – arr. We have used the Array.filter() method to check if this array contains the substring “xy”. We have defined a callback function which checks if an element of the array contains the substring “xy”. All the elements containing the substring are returned in a new array.

In this case, only the string “xyz” contains the substring “xy”. Hence, it is returned as the output.

If no element matches the substring, then an empty array is returned.

Method 4: Use The Array.some() Method To Check If At Least One Element Of The Array Matches The Substring

The Array.some() method takes a callback function as its argument. This callback function can be used to check if an element of the array contains the substring. As soon as one of the elements containing the substring is found, true is immediately returned.

Let’s take an example to understand this method.

Suppose we have an array of strings as follows –

const arr = ["abc", "xyz", "pqr", "lmn"];

Now, let’s use the Array.some() method to check if this array contains the substring “xy”:

const result = arr.some(element => element.includes("xy"));

console.log(result); // true

In the above code, we have defined an array of strings – arr. We have used the Array.some() method to check if this array contains the substring “xy”. We have defined a callback function which checks if an element of the array contains the substring “xy”.

As soon as one element containing the substring is found, true is returned. In this case, it is the string “xyz”. Hence, true is returned as the output.

If no element matches the substring, then false is returned.

Method 5: Use The Array.every() Method To Check If All Elements Of The Array Match The Substring

The Array.every() method takes a callback function as its argument. This callback function can be used to check if all elements of the array contains the substring. As soon as one of the elements does not contain the substring, false is immediately returned.

Let’s take an example to understand this method.

Suppose we have an array of strings as follows –

const arr = ["abc", "xyz", "pqr", "lmn"];

Now, let’s use the Array.every() method to check if this array contains the substring “xy”:

const result = arr.every(element => element.includes("xy"));

console.log(result); // false

In the above code, we have defined an array of strings – arr. We have used the Array.every() method to check if this array contains the substring “xy”. We have defined a callback function which checks if an element of the array contains the substring “xy”.

As soon as one element does not contain the substring, false is immediately returned. In this case, it is the string “abc”. Hence, false is returned as the output.

If all elements match the substring, then true is returned.

Conclusion

In this article, we explored five different methods to check if an array contains a substring in JavaScript. These methods can be used depending on the requirement.

We hope that this article was helpful and you were able to learn something new.

Happy coding!

Leave a Reply