Below is the example of the Array lastIndexOf() method.
The arr.lastIndexOf() method is used to find the index of the last occurrence of the search element provided as the argument to the function.
Parameters: This method accepts two parameters as mentioned above and described below:
- element: This parameter holds the element which index will be return.
- start: This parameter is optional and it holds the starting point of the array, where to begin the search the default value is 0.
Return value: This method returns the index of the first occurrence of the element. If the element cannot be found in the array, then this method returns -1.
- Example 1: In this example the method will searched for the element 2 in that array return that element indesx.
Input : [1, 2, 3, 4, 5].lastIndexOf(2); Output: 1
- Example 2: In this example the method will searched for the element 9 in that array if not found then return -1.
Input : [1, 2, 3, 4, 5].lastIndexOf(9); Output: -1
Code for the above method is provided below:
- Google Chrome 38 and above
- Microsoft Edge 12 and above
- Mozilla Firefox 28 and above
- Safari 8 and above
- Opera 25 and above