Array.prototype.indexOf

Returns the index of the first occurrence of a value in an array, or -1 if it is not present.

Syntax

indexOf(value, fromIndex);

Parameters

Example

["tea", "coffee", "juice"].indexOf("coffee")
1
["tea", "coffee", "juice"].indexOf("milk")
-1
// returns the first found index
["tea", "coffee", "coffee", "coffee", "juice"].indexOf("coffee")
1
// compare to lastIndexOf:
["tea", "coffee", "coffee", "coffee", "juice"].lastIndexOf("coffee")
3

Return value

An index or -1 if the element was not found.

See also