Javascript - Array Array Search

Introduction

indexOf() and lastIndexOf() accepts two arguments: the item to look for and an optional index from which to start looking.

The indexOf() method starts searching from the front of the array (item 0) and continues to the back.

lastIndexOf() starts from the last item in the array and continues to the front.

The methods each return the position of the item in the array or -1 if the item isn't in the array.

An identity comparison is used when comparing the first argument to each item in the array.

The items must be strictly equal as if compared using ===.

Demo

var numbers = [1,2,3,4,5,4,3,2,1];

console.log(numbers.indexOf(4));        //3
console.log(numbers.lastIndexOf(4));    //5

console.log(numbers.indexOf(4, 4));     //5
console.log(numbers.lastIndexOf(4, 4)); //3

var person = { name: "First" };
var people = [{ name: "First" }];

var morePeople = [person];

console.log(people.indexOf(person));     //-1
console.log(morePeople.indexOf(person)); //0

Result