JavaScript | Array indexOf()

arr.indexOf() function is used to find the index of the first occurrence of the search element provided as the argument to the function. The syntax of the function is as follows:

arr.indexOf(searchElement[,index])

Arguments
The first argument to this function is the searchElement which is the value to be searched in the array. Second argument to this function is the optional index argument which defines the starting index in the array from where the element is to be searched. If this argument is not provided then index 0 is taken as the starting index to begin the search as it is the default value.

Return value
This function returns the index of the first occurrence of the searchElement. If the element cannot be found in the array, then this function returns -1

Examples for the above function are provided below:

Example 1: In this example the function indexOf() finds the index of first occurrence of 2 in the array. Since 2 is present at position 0, therefore it returns it as the answer.



var array = [2, 9, 9];
print(array.indexOf(2));

Output:

0

Example 2: In this example the function indexOf() checks for 45 from index 2 in the array. Since it is found at position 3, therefore it returns it as the answer.

var array = [2, 98, 12, 45];

// Here second argument is starting index
// from where we need to search.
print(array.indexOf(45, 2));

Output:

3

Example 3: In this example the function indexOf() checks for 98 from index 2 in the array. Since it is not found, therefore it returns -1 as the answer.

var array = [2, 98, 12, 45];
print(array.indexOf(98,2));

Output:

-1

Example 4: In this example the function indexOf() checks for 100 in the array. Since it is not found, therefore it returns -1 as the answer.

var array = [2, 98, 12, 45];
print(array.indexOf(100));

Output:

-1

Example 5: In this example the function indexOf() checks for 98 in the array. Since it is found at position 1, therefore it returns 1 as the answer.

var array = [2, 98, 12, 98];
print(array.indexOf(98));

Output:

1

Codes for the above function are provided below:



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// JavaScript to illustrate indexOf() function
<script>
function func() {
    var array = [2, 9, 9];
    document.write(array.indexOf(2));
}
func();
</script>

chevron_right


Output:

0

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// JavaScript to illustrate indexOf() function
// with two parameters. 
<script>
function func() {
    var array = [2, 98, 12, 45];
  
    // Here second argument is starting index
    // from where we need to search.
    document.write(array.indexOf(45, 2));
}
func();
</script>

chevron_right


Output:

3

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

//JavaScript to illustrate indexOf() function
<script>
function func() {
    var array = [2, 98, 12, 45];
    document.write(array.indexOf(98,2));
}
func();
</script>

chevron_right


Output:

-1

Program 4:

filter_none

edit
close

play_arrow

link
brightness_4
code

//JavaScript to illustrate indexOf() function
<script>
function func() {
    var array = [2, 98, 12, 45];
    document.write(array.indexOf(100));
}
func();
</script>

chevron_right


Output:

-1

Program 5:

filter_none

edit
close

play_arrow

link
brightness_4
code

//JavaScript to illustrate indexOf() function
<script>
function func() {
    var array = [2, 98, 12, 98];
    document.write(array.indexOf(98));
}
func();
</script>

chevron_right


Output:

1


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.