+ 1
if the array has the item we can get it's index then try to access the item.
So in this process we don't know the index of item but machine can get the index of item.
Suppose,
var a= [1,2,3, 4,5,6,7,8,9,10]
var index1 = a.indexOf(11)
var index2 = a.indexOf(1)
So,
index1 = -1 [It's a garbage value which will be returned when array don't consist the value]
index2 = 0 [the index of 1 in a is 0]
Thus,
We can tell weather a array has the value or not in this way:
if ( a.indexOf(11) >= 0)
console.log("The value is present ")
else
console.log("The value is not present ")
+ 4
Providing an illegal index when referencing an array's element yields `undefined`
Did you mean to find something in the array instead? that is possible even if we don't know the target element's index.
+ 2
suppose the index stored in index2 as shown above.
you can access the item like this:
var item = a[index2]
thus item has the value 1