Answer:
The most common approach to accessing an array is to use a for loop:
var mammals = new Array("cat","dog","human","whale","seal");
var animalString = "";
for (var i = 0; i < mammals. length; i++) {
animalString += mammals[i] + " ";
}
alert(animalString);
Discussion
A for loop can be used to access every element of an array. The array begins at zero, and the array property length is used to set the loop end.
Though support for both indexOf and lastIndexOf has existed in browsers for some time, it’s only been formalized with the release of ECMAScript 5. Both methods take a search value, which is then compared to every element in the array. If the value is found, both return an index representing the array element. If the value is not found, –1 is returned. The indexOf method returns the first one found, the lastIndexOf returns the last one found:
var animals = new Array("dog","cat","seal","walrus","lion", "cat");
alert(animals.indexOf("cat")); // prints 1
alert(animals.lastIndexOf("cat")); // prints 5
Both methods can take a starting index, setting where the search is going to start:
var animals = new Array("dog","cat","seal","walrus","lion", "cat");
alert(animals.indexOf("cat",2)); // prints 5
alert(animals.lastIndexOf("cat",4)); /