which value does the javascript array.indexOf take as index position

时间:2016-10-20 12:46:52

标签: javascript arrays

In the following array, which value is considered as the fourth index/position by javascript, in indexOf() method. Is it "Mango" or "Banana" ?

var fruits = ["Banana", "Orange", "Apple", "Mango", "Banana", "Orange", "Apple"];
var a = fruits.indexOf("Apple", 4);

1 个答案:

答案 0 :(得分:0)

Here is a little tool to play with:

var arr = ["Banana", "Orange", "Apple", "Mango", "Banana", "Orange", "Apple"]
var index = parseInt(prompt("Give an integer between 0 and " + (arr.length - 1)));
alert("At index: " + index + " we find: " + arr[index]);
for (var i = 0; i < arr.length; i++) {
  console.log(i, ":", arr[i]);
}