Open In App

Node.js indexOf() function

Last Updated : 30 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

Similar Reads

Node.js Buffer.indexOf() Method
Buffer is a temporary memory storage which stores the data when it is being moved from one place to another. It is like the array of integers. The Buffer.indexOf() method firstly checks for the input value, if it present in the buffer then it returns the first position (index) from where the value is starting. Syntax: buffer.indexOf( value, start,
2 min read
Difference Between indexOf and findIndex function of array
The task is to differentiate between the indexOf() and findIndex() methods of JavaScript. we're going to discuss both approaches. JavaScript indexOf() Method: This method is used to find the index of the first occurrence of the elements provided for search as the argument to the function. Syntax: arr.indexOf(element[, index]) Parameters: element: T
2 min read
Underscore.js _.indexOf() Function
Underscore.js _.indexOf() gives the index of the element whose position we need to find. It starts to count the position of the elements in the array from 0. If the element is not present in the array then the result will be -1. Syntax: _.indexOf(array, value, [isSorted]);Parameters:array: It is the array, in which we want to find out the index of
3 min read
JavaScript typedArray.indexOf() Method
The typedArray.indexOf() is an inbuilt function in JavaScript which is used to return the index of the element if found in the given typedArray otherwise it returns -1. Syntax: typedarray.indexOf(Element, Index); Parameters: It accepts two parameter which are specified below- Element: It is the element which are being searched for index in the type
2 min read
TypeScript | String indexOf() Method
The indexOf() is an inbuilt function in TypeScript which is used to get the index within the calling String object of the first occurrence of the specified value. Syntax: string.indexOf(searchValue[, fromIndex]) Parameter: This method accepts two parameter as mentioned above and described below . searchValue: This parameter is a string representing
1 min read
TypeScript | Array indexOf() Method
The Array.indexOf() is an inbuilt TypeScript function which is used to find the index of the first occurrence of the search element provided as the argument to the function. Syntax: array.indexOf(searchElement[, fromIndex]) Parameter: This method accepts two parameter as mentioned above and described below: searchElement : This parameter is the Ele
1 min read
Ember.js MutableArray indexOf() Method
Ember.js is an open-source JavaScript framework used for developing large client-side web applications which is based on Model-View-Controller (MVC) architecture. Ember.js is one of the most widely used front-end application frameworks. It is made to speed up development and increase productivity. Currently, it is utilized by a large number of webs
4 min read
Ember.js EmberArray indexOf() Method
Ember.js is an open-source JavaScript framework used for developing large client-side web applications which is based on Model-View-Controller (MVC) architecture. Ember.js is one of the most widely used front-end application frameworks. It is made to speed up development and increase productivity. Currently, it is utilized by a large number of webs
3 min read
Ember.js ArrayProxy indexOf() Method
Ember.js is an open-source JavaScript framework used for developing large client-side web applications which is based on Model-View-Controller (MVC) architecture. Ember.js is one of the most widely used front-end application frameworks. It is made to speed up development and increase productivity. Currently, it is utilized by a large number of webs
3 min read
Ember.js Ember.NativeArray indexOf() Method
Ember.js is an open-source JavaScript framework used for developing large client-side web applications which is based on Model-View-Controller (MVC) architecture. Ember.js is one of the most widely used front-end application frameworks. It is made to speed up development and increase productivity. Currently, it is utilized by a large number of webs
4 min read