Home >>JavaScript Array Reference >JavaScript Array lastIndexOf() Method

JavaScript Array lastIndexOf() Method

JavaScript Array lastIndexOf() Method

JavaScript array lastIndexOf() method is used to find the index of the last occurrence at which a given element can be found in the array. In other words, it search the position of a particular element in a given array. It is case sensitive. It returns -1, if an element is not present in a string.

Syntax:
array.lastIndexOf(item, start)

Parameter Values

Parameter Description
item It Required the item which is searched
start It is optional. It represents the element at which position in the array to start the search. Basically, the Negative values will start at the given position counting from the end, and search to the beginning.

Browser Support

Method Chrome Edge Firefox Safari Opera
lastIndexOf() Yes 9.0 Yes Yes Yes
Here is an example of lastIndexOf() Method:
<html>
<body>
<button onclick="myLastindOf()">Click me</button>
<p id="lastin"></p>
<script>
function myLastindOf() 
{
  var z = ["AB", "BA", "CD", "DC"];
  var a = z.lastIndexOf("CD");
  var b = document.getElementById("lastin");
  b.innerHTML = a;
}
</script>
</body>
</html>
Output:

Example 2:
<html>
<body>
<script> 
function lastindex()
{ 
 var array = [2, 76, 25, 45]; 
 document.write(array.lastIndexOf(45,2)); 
} 
lastindex(); 
</script> 
</body>
</html>
Output: