Finding number of occurrence of string in array of strings in javascript

var array=[[“The end”,“end the”],[“A the”,“no way”],[“yo man”,“man the”]];

function chkthe(arr){
  var result=0;
  var exp=/the/gi;
  for(var i=0;i<arr.length;i++){
    for(var j=0;j<arr[i].length;j++){
      //var result=0;
      //var exp=/the/gi;
      var matc=arr[i][j].match(exp);
  return result;

Hey guys in the upper code I am trying to fetch “the” expression from a multi dimensional array of strings.The result should count a number of expression occurrence and shows on the screen which is, in this case, is ‘4’.I am a noob so please stay easy on me. Thanks in advance :slight_smile:


I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks (key below the ESC) and follow it with a line of three backticks to make easier to read.


Please run this in a browser and open up the browser console (CTRL-SHFT-J on Windows). If you do, you will see an error like Uncaught TypeError: matc.length is not a function… If I remove the parentheses from that, it works for me. Array.prototype.length is a property, not a function.

Whenever you have a problem, one of your first responses should be to open the browser console and look for errors. And then if there are no errors, start using console.log to output stuff to see what is happening.

Let us know if it still isn’t clear.

Happy hunting.