Array.prototype.indexOf()
Returns the first index at which a given element is found, or -1 if absent.
Syntax
arr.indexOf(searchElement, fromIndex) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
searchElement | any | Yes | The value to locate, compared using strict equality (===). |
fromIndex | number | No | Index to start the search from. Negative values count from the end. Defaults to 0. |
Returns
number — The first matching index, or -1 if not found.
Examples
console.log(['a', 'b', 'c', 'b'].indexOf('b'));
Output
1
console.log([1, 2, 3].indexOf(9));
Output
-1
Notes
Uses strict equality, so it cannot find NaN - use `includes` for that. Use
`lastIndexOf` to search from the end.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 1.0 |
| firefox | 1.5 |
| safari | 3.0 |
| edge | 12 |
| node | 0.10 |