
So, the indexOf() method neglects the 0 th index and starts searching from 1 st. The animal array has Dog value but it prints -1 because we are starting search operation at 1 st index position. While we are trying to access the Dog from the array then prints -1. The above example we can see while we are accessing Cow from the array then prints 6 as the index position. Whereas if we pass not existed value to the indexOf() method, it will return -1. It will return the actual index position.

Var indexOfBookNotExisted=searchIndex(book2) ĭocument.write(book1+" position is :"+indexOfBook+"") ĭocument.write(book2+" position is :"+indexOfBookNotExisted) Īs we can observe from the output we can clearly see if we pass existed items from an array is given to the indexOf() method. Searching the index of a book from an array (without mentioning a position): Below example, we do not specify any position within the indexOf() method so, the search starts at the 0 th. The examples of the following are given below: Example #1


Item: Pass the required item from an array.Syntax: Array_Name.indexOf (item,starting position)
