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 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

7

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

8


My Personal Notes arrow_drop_up

Contented with little yet wishing for more

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.