4 examples to Learn JavaScript indexOf

The indexOf method

The indexOf method is used to search in arrays. You have to specify an element to be found in given array and indexOf will return its index in the array if that array contains 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 an array.
  • 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 javascript array indexOf method.
  • If the array contains 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 beginning of an array then use javascript lastIndexOf method (explained at the bottom part with example).

Syntax of using indexOf in javascript

Following is the general syntax of using indexOf in javascript:

Array_name.indexOf(element_to_search, start_index)

Where:

  • Element_to_search: is the element you want to search in given 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 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 array in javascript 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 below:

Experience this example online


You can see when you select New York, Texas, Florida, 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 javascript also allows to let you specify where to start searching by providing 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

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

The JS lastIndexOf works just like 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 javascript indexOf you can specify where to start searching. The only difference is lastIndexOf javascript method will start searching from the end of an array.

See example below of how to use JavaScript’s lastIndexOf method.

Javascript lastIndexOf example

The following example shows how to use the lastIndexOf method.

Experience this example online


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

Also see Javascript Array | Javascript splice | Javascript length

Was this article helpful?

Related Articles

Leave A Comment?