The .indexOf() method returns the first index at which an element can be found. Returns -1 if the element is not found.


array.indexOf(element, startIndex);

The following parameters are used in the .indexOf() method:

  • The element to be searched for in the array.
  • The optional startIndex position to begin searching from. If one is not given, the search starts from the beginning of the array. Negative indices will offset from the end of the array.


In the example below, the index for the number 12 is logged to the console:

const numbers = [6, 12, 8, 10];
const indexOf12 = numbers.indexOf(12);
// Output: 1

If element is not found the result will be -1:

const pizzaToppings = ['pepperoni', 'olives', 'mushrooms'];
const indexOfPineapple = pizzaToppings.indexOf('pineapple');
// Output: -1

Check if color 'blue' is in the colors array starting with the second element:

const colors = ['blue', 'orange', 'pink', 'yellow', 'teal'];
const checkBlue = colors.indexOf('blue', 1);
// Output: -1

Codebyte Example

Multiple matches will only return the first index where a match occurs:

Looking to contribute?

Learn JavaScript on Codecademy