indexOf (object, startAt) Number
public
Returns the index of the given object's first occurrence. If no startAt
argument is given, the starting location to search is 0. If it's negative, will count backward from the end of the array. Returns -1 if no match is found.
let arr = ['a', 'b', 'c', 'd', 'a']; arr.indexOf('a'); // 0 arr.indexOf('z'); // -1 arr.indexOf('a', 2); // 4 arr.indexOf('a', -1); // 4 arr.indexOf('b', 3); // -1 arr.indexOf('a', 100); // -1
Parameters:
-
object
Object
- the item to search for
-
startAt
Number
- optional starting location to search, default 0
Returns:
-
Number
- index or -1 if not found
Please login to continue.