Name

Array.indexOf() — search an array

Availability

ECMAScript 5

Synopsis

array.indexOf(value)
array.indexOf(value, start)

Arguments

value

The value to search array for.

start

An 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

Get JavaScript: The Definitive Guide, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.