English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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().
array.indexOf(element, start)
var fruits = ['Banana', 'Mango', 'Apple', 'Orange']; fruits.indexOf('Apple');Test and see‹/›
The numbers in the table specify the first browser version that fully supports the indexOf() method:
Method | |||||
indexOf() | Yes | Yes | 34 | Yes | 9 |
Parameter | Description |
---|---|
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 |
Return value: | The index of the first occurrence of the element in the array; if not found, return -1 |
---|---|
JavaScript version: | ECMAScript 5 |
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 -1Test and see‹/›