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




indexOf() - FUNCTION


The indexOf() Function is used to return the first occurrence of an element in the Array.


Let us say, we have a Array that contains three names, Mohan, Kriti and Salim. And we want to find the location of Kriti.


Example :



<html>
<body>  
<script>

	var x = ["Mohan", "Kriti", "Salim"]
	var y = x.indexOf("Kriti")
	document.write("The position of Kriti is ",y) 
    
</script>      
</body>
</html>


Output :



  The position of Kriti is 1

So, in the above code we have created a Array and initialised to the variable x.


x = ["Mohan", "Kriti", "Salim"]

Below is how the values are positioned in the Array,

java_Collections

Now, if we see the above diagram, Kriti resides at position/index 1.


And the indexOf() Function is used to find the position of Kriti.


y = x.indexOf("Kriti")

And we get the below output,


The position of Kriti is 1