Name
Array.indexOf() — search an array
Availability
ECMAScript 5
Synopsis
array.indexOf(value)array.indexOf(value,start)
Arguments
valueThe value to search
arrayfor.startAn optional array index at which to begin the search. If omitted, 0 is used.
Returns
The lowest index >= start of
array at which the element is === to value,
or -1 if no such matching element exists.
Description
This method searches array for an
element that is equal to value, and
returns the index of the first element it finds. The search begins
at the array index specified by start, or
at index 0, and continues with successively higher indexes until a
match is found or all elements have been checked. The === operator is used to test equality. The
return value is the index of the first matching element found, or -1
if no match is found.
Example
['a','b','c'].indexOf('b')// => 1['a','b','c'].indexOf('d')// => -1['a','b','c'].indexOf('a',1)// => -1
See Also
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access