The JavaScript indexOf() Method for Arrays and Strings

The JavaScript indexOf() method is used for returning array index values, character or word positions in strings, and more.

This article will step you through how to implement the indexOf() method into your website or web application by exploring a few real-world scenarios.

The JavaScript indexOf() Method Defined

The indexOf() method is defined as follows:

array.indexOf(element, start)
  • The element parameter is required and is the element or value, that will be searched throughout each array index.
  • The start parameter is optional, with a default value of 0, the first value within an array. This is the position in the array where the search will begin.

How to Find Array Index Values

The first way you can use indexOf() is to search for strings in arrays by returning their index value where the string is found. Let's start with an example:

var fruits = ["Apples", "Bananas", "Mangos", "Oranges"];

console.log(fruits.indexOf("Oranges"));
// 3

This example looks for any occurrences of the word "Oranges" in the fruits array. Since this element exists within the array, the associated index value is returned.

Keep in mind, if the same string value were to exist in the array multiple times, only the first index value where the string value exists will be returned:

var fruits = ["Apples", "Bananas", "Mangos", "Oranges", "Kiwis", "Oranges"];

console.log(fruits.indexOf("Oranges"));
// 3

So, even though the word "Oranges" exists in the array twice, only the first index value found will be returned.

If the element does not exist in the array, it would return -1 as the index value, meaning the element was not found in the array:

var fruits = ["Apples", "Bananas", "Mangos", "Oranges"];

console.log(fruits.indexOf("Kiwis"));
// -1

With this in mind, you can check for non-existence of an array value by writing a conditional statement:

if (fruits.indexOf("Kiwis") === -1) {
console.log("Kiwis not found.");
}

This is a great solution when you want to know the exact position of an element in a given array. It gets the job done, and is pretty straightforward.

How to Find a Character or Word Within a String

If you want to find any occurrences of characters or words within strings, the process is similar.

In this example, instead of searching for the value "oranges" in an array, we'll search for it in a string:

var my_string = "I like to eat apples and oranges.";

console.log(my_string.indexOf("oranges"));
// 25

The word "oranges" starts at the 25th character position of the sentence, so 25 is returned as the result.

Similarly to arrays, if the same word exists multiple times, only the index value of the first occurrence will be returned:

var my_string = "I like to eat apples and oranges and more oranges.";

console.log(my_string.indexOf("oranges"));
// 25

And, if a character or word is not found within the string you're searching, -1 will be returned:

var my_string = "I like to eat apples and oranges.";

console.log(my_string.indexOf("kiwis"));
// -1

Searching from a Starting Index Value

You can search for occurrences of array elements or strings from a starting index value by adding an additional argument to JavaScript's indexOf() method:

var fruits = ["Apples", "Bananas", "Mangos", "Oranges"];

console.log(fruits.indexOf("Bananas", 2));
// -1

Normally, the word "Bananas" would be found when searching through the array. However, with this additional argument in the indexOf() method, since we're starting at array index 2, the occurrence of "Bananas" is not found, because it only exists in index 1 of the array.

If the word "Bananas" and "Mangos" were swapped in this example, the array index value returned would be 2.

Case Matters

It's important to remember that searching through arrays or strings is strictly case-sensitive. If, for example, your array or string contains the word "oranges" but you're searching for the capitalized version, "Oranges", -1 will be returned as the result because the correct case was not found within the subject array.

Conclusion

The indexOf() method is useful for determining array index values and character or word positions within strings.

This method is supported by all modern browsers to date:

Browser Support