The JavaScript indexOf method

The indexOf method is used to search in arrays. You have to specify an element to be found in the given array and indexOf method will return its index in the array if that array contains the given element.

If given search element does not exist in the array the indexOf method will return -1 value.

See an example of indexOf method

A few main points about JavaScript indexOf method are:

  • The indexOf method is used to search in arrays.
  • The array search starts from the 0 index.
  • The array search ends at the last element.
  • You can also specify starting index number to start the search from in the array indexOf method.
  • If the array contains the searched element, the indexOf JavaScript method will return the index number of the element.
  • If the element is not found, the JS indexOf will return -1.
  • If more than one elements are found in search the array indexOf will return first found element’s index number only.
  • If you need to search from end to the beginning of an array then use the JavaScript lastIndexOf method (explained at the bottom part with example).

Syntax of using JavaScript indexOf

Following is the general syntax of using the indexOf method:

Array_name.indexOf(element_to_search, start_index)

Where:

  • Element_to_search: is the element you want to search in given the array (Array_name).
  • Start_index: Optional parameter to specify which index number to start searching.

Now we will show you examples with the code of using the indexOf method.

An indexOf method example

Following is a simple demo to check if JavaScript array contains a given element or not. We have created an array of US State names containing four State names.

Then we used the indexOf method to find if the array contains Alaska state or not. Finally, we displayed the returned value in an alert. See yourself by clicking the demo link below:

Experience this example online

You can see, when demo page loads, it is showing an alert with the index value of Alaska State, which is 2 (starting from zero).

Now let us show an example where a user selecting a value and then we will search if the array contains that element or not.

Array in JavaScript contains the element or not by selected element example

This example shows if array in JavaScript contains the element or not selected by the user. The options are given in a dropdown.

As you press the button after selecting the State name, the Javascript indexOf method will search if array in JS contains that State or not. If yes the alert will show its index number in the array. If not found, it will return -1 that will be shown in alert as well.

See the example by clicking the link or image below:

 

Experience this example online

You can see when you select New York, Texas, Florida, and Alaska it will return index value of array in the alert. However, if you select California and Hawaii and press the button, it shows -1 in an alert.

An indexOf method example with start index example

The indexOf method also allows to let you specify where to start searching by providing the index number. The default, as used in above examples is zero index. This example shows how to use start index in array indexOf method.

We are using the same example as above but with array search starting at index 1. Although New York State name exists in the array at 0 index number. The example will show -1 as you select New York in drop down. Check by yourself by clicking the link below:

Experience this example online

You can see, not only California and Hawaii selection shows -1 alert but the New York is also showing -1 because we started searching from 1 index of the array while New York exists at 0 index.

JavaScript lastIndexOf method

The indexOf method is used to search arrays from the start towards the end of an array. If you want to search from the end towards the start of an array then use the JavaScript lastIndexOf method.

The JS lastIndexOf works just like the indexOf method, e.g. if searched element is found it will return the index number. If the element is not found it will return -1.

Just like the indexOf method, you can specify where to start the searching. The only difference is the lastIndexOf JavaScript method will start searching from the end of an array.

See example below of how to use the lastIndexOf method.

An example of lastIndexOf 

The following example shows how to use the lastIndexOf method.

Experience this example online

You can see how to use the lastIndexOf method in the example.

Further Reading: JavaScript Array | JavaScript splice | JavaScript length