Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Node.js indexOf() function

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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 GeeksforGeeks";
const str2 = "to"
 
findIndex(str1, str2);


Output:

8
My Personal Notes arrow_drop_up
Last Updated : 30 Mar, 2023
Like Article
Save Article
Similar Reads
Related Tutorials