indexOf()method is used for searching the array for a specified item, then returning its position.
- The search starts at your specified position, or at the beginning if no position has been specified.
indexOf() Function Explained
indexOf() method is used for searching the array for an item the coder specifies, and returning its position. You can also easily choose at which point of the array should the search begin.
Tip: To perform the search from array's end to start, lastIndexOf() method is used.
See the example provided below. This is how you can search an array for an item
var sampleArray = ["x", "y", "z"]; sampleArray.indexOf(x);
The second argument is named start. Unlike item, it is optional: if you want to start the search from the beginning of the array, you may skip defining this argument. However, in cases when you want the search to begin in a certain position in the array, you should set this parameter.
|item||Required. Defines what the fuction will look for.|
|start||Not required. Defines where the search with start.|
Note: if you set start to have a negative value, the search will start at that position counting from the end.
However, in cases when the array does not feature the item you searched for, you should expect to receive the return value of -1.
|Return Value:||A number which represents the specified item's position.
-1 if the item is not found.