indexOf ExamplesΒΆ

The general syntax for this method is:

arrayName.indexOf(item)

This method returns the index of the FIRST occurence of an item in the array. If the item is not in the array, -1 is returned.

Example

1let charles = [1, 7, 5, 9, 5];
2let otherArray = ['hello', 'world!'];
3
4console.log(charles.indexOf(5));
5
6console.log(otherArray.indexOf('hi'));

Output

2
-1