JavaScript Array indexOf()
Example 1
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.indexOf("Apple");
Try it Yourself »
Definition and Usage
indexOf() returns the first index (position) of a specified value.
indexOf() returns -1 if the value is not found.
indexOf() starts at a specified index and search from left to right.
See Also:lastIndexOf()
Example 2
Start at position 3:
const fruits = ["Banana", "Orange", "Apple", "Mango", "Apple"];
fruits.indexOf("Apple", 4);
Try it Yourself »
Note
In an array, the first element has position 0, the second position 1, and so on.
Browser Support
indexOf() is an ES5 feature (JavaScript 2009).
It is fully supported in all modern browsers:
| Chrome | IE | Edge | Firefox | Safari | Opera |
| Yes | 9.0 | Yes | Yes | Yes | Yes |
JavaScript Version: ECMAScript 5
Syntax
array.indexOf(item, start)
Parameter Values
| item | Required. The value to search for. |
|
| start | Optional. Where to start the search. Negative values start at the given index counting from the end. |
Return Value
| A number. The position of the specified item or -1. |


