Check If An Array of Strings Contains a Substring in JavaScript

Last Updated : 27 Feb, 2026

Working with arrays of strings often requires checking whether any element contains a specific substring. JavaScript provides multiple built-in methods to efficiently perform this check.

  • Use Array.prototype.some() with String.prototype.includes() to return true if at least one string contains the substring.
  • Alternatively, use indexOf() on each string and check if the result is not -1.
  • Choose the method based on readability and browser compatibility requirements.

Here are the different ways to check if an array of strings contains a substring in JavaScript

1. Using includes() and filter() method

We will create an array of strings, then use includes() and filter() methods to check whether the array elements contain a sub-string.

JavaScript
const a = ['Geeks', 'gfg', 'GeeksforGeeks', 'abc'];
const substr = 'eks';
const subArr = a.filter(str => str.includes(substr));

console.log(subArr);
  • The code filters the array to find all strings that contain the substring 'eks'.
  • It uses the filter() method to create a new array (subArr) where each string from array is checked with includes().
  • The includes() method returns true if the string contains 'eks'.
  • The result is stored in subArr and printed.

2. Using includes() method and some() method

We will create an array of strings, and then use includes() and some() methods to check whether the array elements contain a sub-string.

JavaScript
const a = ['Geeks', 'gfg', 'GeeksforGeeks', 'abc'];
const substr = 'eks';
const subArr = a.some(str => str.includes(substr));

console.log(subArr);
  • The code checks if any string in the array contains the substring 'eks'.
  • It uses the some() method, which returns true if at least one string meets the condition (str.includes(substr)), otherwise false.

3. Using indexOf() method

Using indexOf() method that returns the index of the given value in the given collection.

JavaScript
const a = ['Geeks', 'gfg', 'GeeksforGeeks', 'abc'];
const substr = 'eks';

const result = a.some(str => str.indexOf(substr) !== -1);

console.log(result); 
  • array.indexOf(value) checks for an exact element match, not a substring inside elements.
  • It returns -1 if not found, so the correct condition is index !== -1, not just if (index).
  • To check for a substring in array elements, use str.indexOf(substr) !== -1 inside methods like some().

4. Using Lodash _.strContains() Method

Using the Lodash _.strContains() method that return the boolean value true if the given value is present in the given string else it returns false.

JavaScript
let _ = require('lodash-contrib');
let bool = _.strContains("abc", "a");

console.log("The String contains the "
    + "searched String : ", bool); 

Output

true
  • The code uses Lodash's _.strContains() method from the lodash-contrib library to check if the string "abc" contains the substring "a".
  • The result is stored in the variable bool.

5. Using a for of loop

Using a for of loop and indexOf iterates through each string in the array.

JavaScript
const a = ['Geeks', 'gfg', 'GeeksforGeeks', 'abc'];
const substr = 'eks';
let subArr = [];

for (let str of a) {
    if (str.indexOf(substr) !== -1) {
        subArr.push(str);
    }
}

console.log(subArr); 
  • The loop iterates over each string in arr.
  • indexOf(substr) checks if 'eks' is in each string.
  • Matching strings are pushed into subArr, which is then logged.

6. Using the reduce() Method

The reduce method used to accumulate all strings containing the substring into a new array.

JavaScript
const a = ['Geeks', 'gfg', 'GeeksforGeeks', 'abc'];
const substr = 'eks';

const subArr = a.reduce((acc, str) => {
    if (str.includes(substr)) {
        acc.push(str);
    }
    return acc;
}, []);

console.log(subArr);
  • reduce() iterates through each string in the arr.
  • For each string, it checks if 'eks' is included using includes().
  • If it is, the string is pushed into the accumulator array (acc).
  • The result is an array of matching strings, which is stored in subArr.

7. Using RegExp.test() Method

The RegExp.test() method is an effective way to check if a substring exists within any string in an array.

JavaScript
const a = ['Geeks', 'gfg', 'GeeksforGeeks', 'abc'];
const substr = 'eks';
const regex = new RegExp(substr, 'i');
const subArr = a.filter(str => regex.test(str));

console.log(subArr);
  • new RegExp(substr, 'i') creates a regular expression for 'eks' with the 'i' flag to make the search case-insensitive.
  • filter() checks each string in arr with regex.test(str) to see if it matches.
  • The resulting array, subArr, contains the strings that include 'eks'
Comment