JavaScript Array indexOf()
In this post, we will discuss indexOf() method available in JavaScript Array.
indexOf()
The indexOf() method is used to return the index position of particular element present in the array. if there are multiple similar elements, then first occurrence index is returned. If the element not found in the array, -1 is returned.
Syntax:
array_name.indexOf(element,start)
Here, 'array_name' is the name of the array. It takes two parameters.
- First parameter is the element
- Second parameter specifies the start position in which the element must be searched.
Example 1:-
Let's create an array that hold 10 subjects and return indices of particular elements.
<html>
<body>
<script>
// Create an array that hold 10 strings
const subjects1 =["php","html","node.js","java","jsp","jsp","php","php","java","php"];
document.writeln("Subjects: <br>");
document.writeln(subjects1);
document.writeln("<br>");
document.writeln("<br>");
// Return the index of "php"
document.writeln("php index: "+subjects1.indexOf("php")+"<br>");
// Return the index of "jsp"
document.writeln("jsp index: "+subjects1.indexOf("jsp")+"<br>");
// Return the index of "java"
document.writeln("java index: "+subjects1.indexOf("java")+"<br>");
// Return the index of "big-data"
document.writeln("big-data index: "+subjects1.indexOf("big-data")+"<br>");
</script>
</body>
</html>
Output:
Subjects:
php,html,node.js,java,jsp,jsp,php,php,java,php
php index: 0
jsp index: 4
java index: 3
big-data index: -1
We returned index positions of first occurrence elements:
- 'php' is present at first position.
- 'jsp' is present at fifth position.
- java' is present at fourth position.
- 'big-data' doesn't exists in the array. So, -1 is returned.
Example 2:-
Let's create an array that hold 10 subjects and return indices of particular elements from particular position.
<html>
<body>
<script>
// Create an array that hold 10 strings
const subjects1 =["php","html","node.js","java","jsp","jsp","php","php","java","php"];
document.writeln("Subjects: <br>");
document.writeln(subjects1);
document.writeln("<br>");
document.writeln("<br>");
// Return the index of "php" from 5th position
document.writeln("php index from 5th position: "+subjects1.indexOf("php",5)+"<br>");
// Return the index of "jsp" from 5th position
document.writeln("jsp index from 5th position: "+subjects1.indexOf("jsp",5)+"<br>");
// Return the index of "java" from 8th position
document.writeln("java index from 8th position: "+subjects1.indexOf("java",8)+"<br>");
// Return the index of "html from 8th position"
document.writeln("html index from 8th position: "+subjects1.indexOf("html",8)+"<br>");
</script>
</body>
</html>
Output:
Subjects:
php,html,node.js,java,jsp,jsp,php,php,java,php
php index from 5th position: 6
jsp index from 5th position: 5
java index from 8th position: 8
html index from 8th position: -1
We returned index positions of first occurrence elements:
- 'php' index is 6 from 5th position.
- 'jsp' index is 5 from 5th position.
- 'java' index is 8 from 8th position.
- 'html' does not exist from 8th position. So, -1 is returned.
Example 3:-
Let's create an array that hold 5 integers and return indices of particular values from particular position.
<html>
<body>
<script>
// Create an array that hold 5 integers
const marks =[12,34,56,34,12];
document.writeln("Marks: <br>");
document.writeln(marks);
document.writeln("<br>");
document.writeln("<br>");
// Return the index of 34 searching from start-0
document.writeln("34 index searching from start-0: "+marks.indexOf(34,0)+"<br>");
// Return the index of 12 searching from start-0
document.writeln("12 index searching from start-0: "+marks.indexOf(12,0)+"<br>");
// Return the index of 34 searching from start-2
document.writeln("34 index searching from start-2: "+marks.indexOf(34,2)+"<br>");
// Return the index of 12 searching from start-2
document.writeln("12 index searching from start-2: "+marks.indexOf(12,2)+"<br>");
</script>
</body>
</html>
Output:
Marks:
12,34,56,34,12
34 index searching from start-0: 1
12 index searching from start-0: 0
34 index searching from start-2: 3
12 index searching from start-2: 4