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