How to use indexOf() method In Javascript
In this approach, we are using indexOf() method that returns the index of the given value in the given collection.
Example: This example shows the implementation of the above-explained approach.
const arr = "w3wiki"
const substr = 'eks';
const subArr = arr.indexOf(substr);
if (subArr) { console.log("true") }
else { console.log("false") }
Output
true
Check an array of strings contains a substring in JavaScript
Checking if an array of strings contains a substring in JavaScript involves iterating through each string in the array and examining if the substring exists within any of them. This process ensures efficient detection of the substring’s presence within the array.
These are the following ways to do this:
Table of Content
- Using includes() method and filter() method
- Using includes() method and some() method
- Using indexOf() method
- Using Lodash _.strContains() Method
- Using a for of loop
Contact Us