Count occurrences of a word in string | Javascript program to count the number of occurrence of a word in the given string ; split the string by spaces in a ; search for pattern in a ; if match found increase count ; Driver code
function countOccurrences ( str , word ) { let a = str . split ( " " ) ; let count = 0 ; for ( let i = 0 ; i < a . length ; i ++ ) { if ( word == ( a [ i ] ) ) count ++ ; } return count ; } let str = " " ; let word = " " ; document . write ( countOccurrences ( str , word ) ) ;