JavaScript Array indexOf()
Example
Search an array for the item "Apple":
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.indexOf("Apple") // Returns 2
Try it Yourself »
More "Try it Yourself" examples below.
Definition and Usage
The indexOf() method searches an array for a specified item and returns its position.
The search will start at the specified position (at 0 if no start position is specified), and end the search at the end of the array.
indexOf() returns -1 if the item is not found.
If the item is present more than once, the indexOf method returns the position of the first occurence.
Note: The first item has position 0, the second item has position 1, and so on.
Tip: If you want to search from end to start, use
lastIndexOf()
Browser Support
indexOf() is fully supported in all modern browsers:
| Chrome | IE | Edge | Firefox | Safari | Opera |
| Yes | 9.0 | Yes | Yes | Yes | Yes |
Syntax
array.indexOf(item, start)
Parameter Values
| Parameter | Description |
|---|---|
| item | Required. The item to search for |
| start | Optional. Where to start the search. Negative values will start at the given position counting from the end, and search to the end. |
Technical Details
| Return Value: | A Number, representing the position of the specified item, otherwise -1 |
|---|---|
| JavaScript Version: | ECMAScript 5 |
More Examples
Example
Search an array for the value "Apple", starting at position 3:
const fruits = ["Banana", "Orange", "Apple", "Mango", "Apple"];
fruits.indexOf("Apple", 4) // Returns 4
Try it Yourself »

