Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   HTML   
   CSS   
   JAVA SCRIPT   
   JQUERY   




lastIndexOf() - FUNCTION


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.


Example :



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


Output :



  The position of last occurence of Mohan is 2

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,

JavaScript lastIndexOf()-Function

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