indefOf

What is indefOf?

In JavaScript, the indexOf() method is used to find the index of a specified value in a string or an array. The method returns -1 if the value is not found.

The indexOf() method can be used with both strings and arrays. When used with strings, it returns the index of the first occurrence of the specified value. When used with arrays, it returns the index of the first occurrence of the specified value, or -1 if the value is not found.

Example:


var str = "Hello world!";
var n = str.indexOf("world");

// Output: 6
console.log(n);

var arr = [1, 2, 3, 4, 5];
var m = arr.indexOf(3);

// Output: 2
console.log(m);

In the above example, indexOf() method is used to find the index of "world" in the string "Hello world!". The method returns 6 as "world" starts at index position 6 in the string.

Similarly, the indexOf() method is used to find the index of 3 in the array [1, 2, 3, 4, 5]. The method returns 2 as 3 is present at index position 2 in the array.

Multiple Ways to Use indexOf():

The indexOf() method can be used with different parameters to get different results.

  • indexOf(searchValue): This method returns the index of the first occurrence of the specified value in a string or an array. If the value is not found, it returns -1.
  • indexOf(searchValue, fromIndex): This method returns the index of the first occurrence of the specified value in a string or an array, starting the search at the specified index. If the value is not found, it returns -1.
  • lastIndexOf(searchValue): This method returns the index of the last occurrence of the specified value in a string or an array. If the value is not found, it returns -1.
  • lastIndexOf(searchValue, fromIndex): This method returns the index of the last occurrence of the specified value in a string or an array, searching from right to left starting at the specified index. If the value is not found, it returns -1.

Example:


var str = "Hello world!";
var n1 = str.indexOf("o");
var n2 = str.indexOf("o", 5);
var n3 = str.lastIndexOf("o");
var n4 = str.lastIndexOf("o", 5);

// Output: 4
console.log(n1);

// Output: 7
console.log(n2);

// Output: 7
console.log(n3);

// Output: 4
console.log(n4);

In the above example, we have used different parameters with the indexOf() and lastIndexOf() method to get different results.

The first indexOf() method returns the index of the first occurrence of "o" in "Hello world!" which is 4.

The second indexOf() method returns the index of the first occurrence of "o" in "Hello world!" starting from index position 5, which is 7.

The lastIndexOf() method returns the index of the last occurrence of "o" in "Hello world!" which is 7.

The second lastIndexOf() method returns the index of the last occurrence of "o" in "Hello world!" searching from right to left starting at index position 5, which is 4.

Conclusion:

The indexOf() method is a powerful tool in JavaScript for finding the index of a specified value in a string or an array. By using different parameters, we can get different results and manipulate string and array data more efficiently.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe