Home >>JavaScript Array Reference >JavaScript Array indexOf() Method

JavaScript Array indexOf() Method

JavaScript Array indexOf() Method

The JavaScript array indexOf() method in JavaScript is used to search the position of a particular element provided as the argument in a given array. This is case-sensitive method. The first element in the index position in an array is always start with zero and returns -1, if an element is not present in an array.

Syntax:
array.indexOf(item, start)

Parameter Values

Parameter Description
item It is Required. The item which is searched
start It is optional. It represents the element at which position in the array to start the search. Negative values will start at the given position.

Browser Support

Method Chrome Edge Firefox Safari Opera
indexOf() Yes 9.0 1.5 Yes Yes
Here is an example of Array indexOf() Method:
<html>
<body>
<button onclick="myIndexof()">Click me</button>
<p id="indexof1"></p>
<script>
function myIndexof()
{
  var Colors = ["Red", "Blue", "Pink", "Green"];
  var C = Colors.indexOf("Green");
  document.getElementById("indexof1").innerHTML = C;
}
</script>
</body>
</html>
Output:

Example 2:
<html>
<body>
<script> 
function inOf() 
{ 
 var array = [4, 65, 12, 55]; 
 ocument.write(array.indexOf(65, 4)); 
} 
inOf(); 
</script> 
</body>
</html>
Output:

No Sidebar ads