Javascript examples for Array:lastIndexOf
The lastIndexOf() method searches the array for the specified item, and returns its position.
The search starts at the specified position, or at the end if start position is not specified, and then search towards the beginning of the array.
If the item is present more than once, the lastIndexOf method returns the position of its last occurrence.
array.lastIndexOf(item, start);
Parameter | Description |
---|---|
item | Required. The item to search for |
start | Optional. Where to start the search. |
A Number, representing the position of the specified item, otherwise -1
<!DOCTYPE html> <html> <body> <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> function myFunction() {// w w w. j a v a 2s.com var fruits = ["a","b","c","d","e", "a","b","c","d","e"]; var a = fruits.lastIndexOf("a", 4); var x = document.getElementById("demo"); x.innerHTML = a; } </script> </body> </html>