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.
<html>
<body>
<script>
var x = ["Mohan", "Kriti", "Salim"]
var y = x.indexOf("Kriti")
document.write("The position of Kriti is ",y)
</script>
</body>
</html>
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,
-Function1.png)
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