Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Node.js indexOf() function

  • Last Updated : 13 Oct, 2021

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 to be search.
  • str2: It holds the search string.

Return Value: The function returns the index of the passed parameter.

Below programs demonstrate the working of indexOf() function:

Program 1:




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

Output:

7

Program 2:




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

Output:

8

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!