0

got that code:

var itemGrid = [3,4,2,11,1,3,5,8,6];
var a = itemGrid.indexOf(0);

a is everytime -1. It should be 3. What did I do wrong?

fiddle: http://jsfiddle.net/okg9g4tt/

Standard
  • 1,450
  • 17
  • 35
  • its because you are searching for 0 in the array – Rinor Dreshaj May 28 '15 at 11:40
  • array.indexOf(n) returns the position of n in the array. if n is not in the array it returns -1. in this case 0 is not present so -1. while array[n] returns value present at nth position in the array – Strikers May 28 '15 at 11:41
  • `indexOf` will give you the index of(!) the element `0` ... which is not part of the array, so the result is `-1`. If you want to get the zero index element of the array, just do `var a = itemGrid[0]` – devnull69 May 28 '15 at 11:41
  • if you want to get the first value which is 3 u should do itemGrid[0] and not with the indexOf(0) – Rinor Dreshaj May 28 '15 at 11:44
  • whoops. Thank you all. – Standard May 28 '15 at 11:49

9 Answers9

5

There is a misunderstanding.

indexOf will not get the element at index 0, it will

return the first index at which a given element can be found in the array, or -1 if it is not present.

If you want the element at index 0, you should simply do itemGrid[0]

AmmarCSE
  • 30,079
  • 5
  • 45
  • 53
3

This is becuase 0 is not a value in your initial array. The function then returns -1. (see docs)

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

To return the value at index 0, use itemGrid[0]

IMPORTANT NOTE:

.indexOf() is not supported by IE8 and below so pay attention if you plan to support these browsers.

Yotam Omer
  • 15,310
  • 11
  • 62
  • 65
2

0 is not an element of that array. So its index is being returned as -1;

probably you want to have itemGrid[0] which will return 3

kakon
  • 701
  • 6
  • 18
1

Should be:

itemGrid[0]

indexOf(0) would return the position of the value 0 in the array, but there is no 0 in the array.

Ted Nyberg
  • 7,001
  • 7
  • 41
  • 72
1

indexOf returns index of found element. Here -1 is returned because 0 is not found in your array.

NOTE: indexOf will get you the position of the element if found, so when you do itemGrid.indexOf(0); , it means you are looking for the position of the element '0' which is not present in your array and hence it is returning -1

Abhinav
  • 8,028
  • 12
  • 48
  • 89
1

a is everytime -1. It should be 3. What did I do wrong?

var itemGrid = [3,4,2,11,1,3,5,8,6];
var a = itemGrid[0];   // 3
ozil
  • 6,930
  • 9
  • 33
  • 56
0

array.indexOf(n) returns the position of n in the array. if n is not in the array it returns -1. in this case 0 is not present so -1.

while array[n] returns value present at nth position in the array

Strikers
  • 4,698
  • 2
  • 28
  • 58
0

"0" is not present in the Array, so it will return -1.

Megh Vidani
  • 635
  • 1
  • 7
  • 22
0

IndexOf (IndexOf<T>(T[], T))

Searches for the specified object and returns the index of its first occurrence in a one-dimensional array.

your example tries to find the first indexof 0 returning -1, meaning no 0's where found.

To return the first index in your array use the indexer ItemGrid[0]

Shane_Yo
  • 770
  • 8
  • 24