JavaScript Array indexOf() method

The JavaScript array indexOf() method is used to search the specified element in the given array and returns the index of the first match.

Syntax:

array.indexOf (element,index)

Parameters:
element: It is required and represents the element which have to be searched.
index: It is optional and represent the index position from where search starts.

Returns:
Index of the first match for a specific element.

Example:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>  
var a = ["GOLD","SILVER","DIAMOND","RUBY","PLATINUM"]	 
var result=a.indexOf("DIAMOND");  
document.writeln(result);  
</script>  
</body>
</html>

Output:

2
Please Share