Find all occurrences of a word in array in JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

We are required to write a JavaScript function that takes in an array of literals as the first argument and a string as the second argument. Our function should return the count of the number of times that string (provided by second argument) appears anywhere in the array.


The code for this will be −

const arr = ["word", "a word", "another word"];
const query = "word";
const findAll = (arr, query) => {
   let count = 0;
   count = arr.filter(el => {
      return el.indexOf(query) != -1;
   return count;
console.log(findAll(arr, query));


And the output in the console will be −

Published on 21-Nov-2020 14:33:15