Open In App

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:

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



Example 1: 




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

Output:

7

Example 2: 




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

Output:

8
Article Tags :