Array lastIndexOf() Method - Javascript Array

Javascript examples for Array:lastIndexOf

Description

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.

Syntax


array.lastIndexOf(item, start);

Parameter Values

Parameter Description
item Required. The item to search for
start Optional. Where to start the search.

Return Value:

A Number, representing the position of the specified item, otherwise -1

Demo Code

ResultView the demo in separate window

<!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>

Related Tutorials