The lastIndexOf() Function is used to return the last occurrence of an element in the Array.
Let us say, we have a Array that contains four names, Mohan, Kriti, Mohan and Salim. And we want to find the location of the last occurence of Mohan.
<html>
<body>
<script>
var x = ["Mohan", "Kriti", "Mohan", "Salim"]
var y = x.lastIndexOf("Mohan")
document.write("The position of last occurence of Mohan is ",y)
</script>
</body>
</html>
So, in the above code we have created a Array and initialised to the variable x.
var x = ["Mohan", "Kriti", "Mohan" "Salim"]
Below is how the values are positioned in the Array,
-Function1.png)
Now, if we see the above diagram, the last occurence of Mohan is at position/index 2.
And the lastIndexOf() Function is used to find the position of Mohan.
y = x.lastIndexOf("Mohan")And we get the below output,
The position of last occurence of Mohan is 2