JavaScript > Arrays

Array indexof search in JavaScript

How to search and return the position of a specified element in an array in JavaScript?


In previous posts, we learnt about Sorting, Adding, Slicing, Reversing, Joining array in JavaScript.

Array indexOf

The Array indexOf {indexOf() method} searches in the array for a specified element and return it's position. The parameters for Array indexOf is 'item' and 'start'. If we give only 'item' the search will start from the beginning of an array and returns the specified item position. If we give both 'item' and 'start', the serach will start from the specified position in 'start'. In case if the specified item is not found in the search, the Array indexOf returns '-1' as output.

NOTE: The position for the first item is '0', the position for the second item is '1', like that it continues and so on.

In JavaScript, by using indexOf() method we can search and return the position of a specified element.

EXAMPLE: Searching for the position of "India" in an array 

<p>Click the below button to know the position of India.</p>
<input type="button" onclick="myPosition()" value="Find"/>
<p id="myId"></p>

<script>
    function myPosition() {
        var a = ["France", "India", "Darziling", "China", "Bangladesh", "Abudhabi"];
        var b = a.indexOf("India");
        document.getElementById("myId").innerHTML = b;
    }
</script>

In the above code snippet we have given Id as "myId"to the second <p> element in the HTML code. There is a function myPosition() in the<script>block which is connected to the onclick of the HTML button and array with value "["France", "India", "Darziling", "China", "Bangladesh", "Abudhabi"]" to the variable a. We need to return the position of a specified item ("India") by searching in an array, for that we are using indexOf() method. Onclick of the button "Find" in the HTML code fires the function myPosition() in the <script> block,  at the same time indexOf() method search for an element "India" in an array and return the position of a element as output.

OUTPUT

EXAMPLE: Starting for the position of "India" in an array, starting the search position at 3.

<p>Click the below button to know the position of India, when the start position is 3.</p>
<button onclick="myFunction()">Find</button>
<p id="myId"></p>

<script>
    function myFunction() {
        var a = ["France", "India", "Darziling", "China", "Bangladesh", "India", "Abudhabi"];
        var b = a.indexOf("India", 3);
        document.getElementById("myId").innerHTML = b;
    }
</script>

Array indexOf with 'item' and 'start'

In the above code snippet we have given 'item' and 'start' to the indexOf() method. Onclick of the button "Find" in the HTML code fires the function myPosition() in the <script> block,  at the same time indexOf() method starts searching from the start position '3' for an item "India" in an array and return the position of a element as output.

OUTPUT

EXAMPLE: Searching for the position of an element which is not present in an array.

<p>Click the below button to know the output for an search which is not presented in an array.</p>
<input type="button", onclick="myFunction()" value="Find"/>
<p id="myId"></p>

<script>
    function myFunction() {
        var a = ["France", "Darziling", "China", "Bangladesh", "Abudhabi", "Sharja"];
        var b = a.indexOf("India");
        document.getElementById("myId").innerHTML = b;
    }
</script>

Searching for an element which is not present in an Array

In the above code snippet we have given an item to the indexOf() method which is not present in an array. Onclick of the button "Find" in the HTML code fires the function myFunction() in the <script> block, at the same time indexOf() method searches for an specified item, the item which we given to indeOf() method is not present in an array, so this method returs '-1' as output.

OUTPUT

 Views: 5274 | Post Order: 163



Write for us






Hosting Recommendations