Node.js indexOf() function

The indexOf() function is a string function from Node.js which is used to find a string with another string. 

Syntax:

str1.indexOf( str2 )

Parameter: This function uses two parameters as mentioned above and described below:

  • str1: It holds the string content where another string is to be searched.
  • str2: It holds the search string.

Return Value: The function returns the index of the passed parameter. The below programs demonstrate the working of the indexOf() function: 

Example 1: 

javascript




function findIndex(str) {
    let index = str.indexOf("awesome");
    console.log(index);
}
 
const str = "gfg is awesome";
 
findIndex(str);


Output:

7

Example 2: 

javascript




function findIndex(str1, str2) {
    let index = str1.indexOf(str2);
    console.log(index);
}
 
const str1 = "Welcome to w3wiki";
const str2 = "to"
 
findIndex(str1, str2);


Output:

8