Tutorialspoint.dev

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:

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

Output:

0

Program 2:

// 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>

Output:

3

Program 3:

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

Output:

-1

Program 4:

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

Output:

-1

Program 5:

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

Output:

1


This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter