top of page

Program to find a word in JavaScript

This article will show you the steps to find a word from a given string in JavaScript using search_word



Code:

function search_word(text, word){
    
    var x = 0, y=0;
   
    for (i=0;i< text.length;i++)
        {
        if(text[i] == word[0])
            {
            for(j=i;j< i+word.length;j++)
               {
                if(text[j]==word[j-i])
                  {
                    y++;
                  }
                if (y==word.length){
                    x++;
                }
            }
            y=0;
        }
    }
   return "'"+word+"' was found "+x+" times.";
}
console.log('String - Technology is best when it brings people together');
console.log(search_word('Technology is best when it brings people together', 'Technology'));

Output:




The Tech Platform

1 comment
bottom of page