Indexof JavaScript Array
In this article, we will learn about indexof the javascript array.
IndexOf() Method:
It returns an index of the first occurrence of a given search element.
Syntax:
indexOf(searchElement:*):number
Where:
- searchElement: indicates element to be searched
- number : indicates an index of the first occurrence of the element to be searched
Ex:
var studNames = ["Ram", "Ravi", "Raju", "Raghu", "Gopal"];
document.write(studNames.indexOf("Ravi")); //1
- In this first line above code, we have created an array variable “studNames” and assigned five string values.
- Then we find the index value of a particular string in “studNames”, we need to use the indexOf() operation. The index of the value to be found must be given in the function