How to search a Javascript array from start

Description

Array indexOf() has the following syntax:


indexOf(item_To_Look_For, optional_Index_To_Start_With)

indexOf() starts searching from the front of the array. indexOf() returns the position of the item in the array or -1 if the item isn't in the array.

An identity comparison is used during comparing.

Example


var numbers = [1,2,3,4,5,4,3,2,1]; 
console.log(numbers.indexOf(4)); //3 
console.log(numbers.indexOf(4, 4)); //5 
//from ww  w .  ja  v a 2 s  .  com
var person = { name: "JavaScript" }; 
var people = [{ name: "JavaScript" }]; 

var morePeople = [person]; 
console.log(people.indexOf(person)); //-1 

The code above generates the following result.





















Home »
  Javascript »
    Javascript Reference »




Array
Canvas Context
CSSStyleDeclaration
CSSStyleSheet
Date
Document
Event
Global
History
HTMLElement
Input Element
Location
Math
Number
String
Window