Onjsdev

Share


JavaScript indexOf Method


By onjsdev

Jan 6th, 2024

Javascript indexOf() method is used for finding the index of a specific element within an array or a substring within a string. If the element is not found, it returns -1.

Syntax

The indexOf method can be used with both arrays and strings.

array.indexOf(searchValue, fromIndex)
string.indexOf(searchValue, fromIndex)

It takes two parameters:

  • searchValue: element in the array or the substring to search for
  • fromIndex (optional): the index to start the search from.

Examples with Arrays

Let's take a look at an example that demonstrates the use of indexOf with an array:

const fruits = ['apple', 'banana', 'orange', 'apple', 'mango']; 
const index = fruits.indexOf('apple'); 
console.log(index); // Output: 0

In this example, the indexOf method is invoked on the fruits array to determine the position of the first instance of the string apple. Since apple is present at index 0, the method returns 0.

Here's an another example:

const fruits = ['apple', 'banana', 'orange', 'mango']; 
const index = fruits.indexOf('grape'); 
console.log(index); // Output: -1

In this case, grape is not present in the fruits array, so the method returns -1.

You can also provide the fromIndex parameter, so that the search will begin at the specified index and move towards the end of the array.

Here's an example:

const fruits = ['apple', 'banana', 'orange', 'apple', 'mango']; 
const index = fruits.indexOf('apple', 1); 
console.log(index); // Output: 3

In this example, the search for apple starts from index 1. The method finds the first occurrence of apple after index 1, which is at index 3, so it returns 3.

Examples With Strings

The indexOf() method can also be used with strings to find the index of a substring.

const sentence = 'The quick brown fox jumps over the lazy dog'; 
const index = sentence.indexOf('fox');
console.log(index); // Output: 16

In this example, the indexOf() method is called on the sentence string to find the index of the substring fox. The method returns 16 since the substring starts at index 16 in the string.

Here's another example:

const sentence = 'The quick brown fox jumps over the lazy dog'; 
const index = sentence.indexOf('cat');
console.log(index); // Output: -1

In this case, the substring cat is not present in the sentence string, so the method returns -1.

The indexOf() method with strings also accepts the fromIndex parameter, so that the search begins at the specified index.

// Searching for the occurrence of 'o' starting from index 4
let str = "Hello, world!";
let index = str.indexOf("o", 4);

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

Negative fromIndex (Change the Direction Of The Search)

The indexOf() method also supports negative values for the fromIndex parameter, which allows you to start the search from the end of the array or string.

Here's how the negative fromIndex parameter works:

  • The index is counted from the end of the array or string, with -1 representing the last element or character, -2 representing the second-to-last, and so on.
  • If the calculated index is less than 0 after applying the negative fromIndex, the entire array or string is searched.

Here's an example to illustrate the use of a negative fromIndex:

let arr = [1, 2, 3, 4, 5];

// Using a negative fromIndex
let index = arr.indexOf(3, -2);
console.log(index); // Output: 2

// Searching for an element that is not in the specified range
let notFoundIndex = arr.indexOf(1, -4);
console.log(notFoundIndex); // Output: 0 (because 1 is at index 0)

In the first example, arr.indexOf(3, -2) starts the search from the second-to-last element (4), and it finds 3 at index 2.

In the second example, arr.indexOf(1, -4) starts the search from the fourth-to-last element (2), and it finds 1 at index 0.

Lastly, here's a table summarizing the important aspects of the indexOf() method:

Method SyntaxDescription
array.indexOf(searchElement)Searches for searchElement in the array and returns the index of the first occurrence, or -1 if not found.
string.indexOf(searchValue)Searches for searchValue in the string and returns the index of the first occurrence, or -1 if not found.
Parameters:
searchElementThe element to search for in the array.
searchValueThe string to search for in the string.
fromIndex(Optional) The index at which to start the search. Default is 0.
Return Value:
Index of the first occurrenceReturns the index of the first occurrence of the specified element or string. Returns -1 if not found.
Examples:
let arr = [1, 2, 3, 4, 5];arr.indexOf(3); returns 2 because 3 is at index 2.
let str = 'Hello, World!';str.indexOf('World'); returns 7 because 'World' starts at index 7.
Note:If fromIndex is provided, the search starts from that index. If negative, it is treated as counting from the end of the array or string. If fromIndex is greater than or equal to the length, -1 is returned.

Conclusion

The Javascript indexOf method is a handy tool for finding the index of a specific element within an array or a substring within a string.

If you want to learn more about javascript array and string methods, then you can take a look at the following articles:

Thank you for reading.