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:
function findIndex(str) { var index = str.indexOf("awesome"); console.log(index); } var str = "gfg is awesome"; findIndex(str); |
chevron_right
filter_none
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); |
chevron_right
filter_none
Output:
8
Recommended Posts:
- Nodejs | Automatic restart NodeJs server with nodemon
- Difference Between indexOf and findIndex function of array
- Nodejs | DNS
- Nodejs | Jimp
- Nodejs | DNS | setServers()
- Web-Socket in NodeJS
- Dockerizing a simple Nodejs app
- Nodejs | Web Crawling using Cheerio
- Encrypting Data in NodeJS
- Difference between NodeJS and AngularJS
- Cowsay in Nodejs using Requests library
- NodeJs - Handling invalid routes
- Signup Form Using Nodejs and MongoDB
- Nodejs – Connect Mysql with Node app
- Nodejs - Connect MongoDB with Node app using MongooseJS
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.


