JavaScript Array.prototype.indexOf() Method
The indexOf() method returns the first index at which a value is found in an array, or -1 if it isn't there.
Description
The indexOf() method returns the first index at which a value appears, using strict equality (===), or -1 if it isn't present. Because of the strict comparison, it can't find NaN.
If you only need to know whether the value is there, includes() is clearer. To search with a condition rather than an exact value, use findIndex().
Its main advantage over includes() is that it tells you where the value is, so you can then remove it with splice(index, 1) or replace it. Remember to check for -1 before using the result as an index. The fromIndex argument lets you continue searching after a previous match to find every occurrence.
Syntax
array.indexOf(searchElement)
array.indexOf(searchElement, fromIndex)Parameters
- searchElementThe value to look for.
- fromIndexOptional. The index to start searching from.
Return Value
The first index of the value, or -1 if it isn't found.
Example
Browser Support
The following information will show you the current browser support for the Array.prototype.indexOf() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





