English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript Array indexOf() Method

 JavaScript Array Object

indexOf()The method returns the first index where the given element can be found in the array.

If the element is not found, it will return-1.

If the element exists multiple times, it will return the position of the first occurrence.

If you want to search from the end, please uselastIndexOf()method.

Note:For more information on String methods, seeString.indexOf().

Syntax:

array.indexOf(element, start)
var fruits = ['Banana', 'Mango', 'Apple', 'Orange'];
fruits.indexOf('Apple');
Test and see‹/›

Browser Compatibility

The numbers in the table specify the first browser version that fully supports the indexOf() method:

Method
indexOf()YesYes34Yes9

Parameter Value

ParameterDescription
element(Required) The element to locate in the array
start(Optional) The index of the element to start searching from. The default value is 0

Technical Details

Return value:The index of the first occurrence of the element in the array; if not found, return -1
JavaScript version:ECMAScript 5

More examples

From index2Start searching from:

var fruits = ['Banana', 'Mango', 'Apple', 'Orange'];
fruits.indexOf('Mango', 2);
Test and see‹/›

If the given parameter does not exist in the array, it will return-1:

var fruits = ['Banana', 'Mango', 'Apple', 'Orange'];
fruits.indexOf('Beer');// Back -1
Test and see‹/›

 JavaScript Array Object