Array.indexOf Function
Searches for the specified element of an Array object and returns its index. This function is static and can be invoked without creating an instance of the object.
var indexVar = Array.indexOf(array, item, start);
The following example shows how to find the index location of a specified item using the indexOf function. The index returned is the first occurrence of the item specified in item. You can find the next occurrence of item by calling the function again and specifying a starting index value larger than the index of the found element.
var a = ['red', 'blue', 'green', 'blue']; var myFirstIndex = Array.indexOf(a, "blue"); // View the results: "1" alert("myFirstIndex: " + myFirstIndex); var mySecondIndex = Array.indexOf(a, "blue", (myFirstIndex + 1) ); // View the results: "3" alert("mySecondIndex: " + mySecondIndex);
Show: