JavaScript - indexOf() method

revision:


Catgory : array

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

Syntax :

        indexOf(searchElement)
        indexOf(searchElement, fromIndex)
        

Parameters:

searchElement : element to locate in the array.

fromIndex : optional. Zero-based index at which to start searching, converted to an integer. Negative index counts back from the end of the array — if "fromIndex < 0", "fromIndex + array.length" is used. Note, the array is still searched from front to back in this case. If "fromIndex < -array.length" or "fromIndex is omitted", 0 is used, causing the entire array to be searched. If "fromIndex >= array.length", the array is not searched and -1 is returned.

Examples :

            const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];
            console.log(beasts.indexOf('bison'));
            // Expected output: 1
            // Start from index 2
            console.log(beasts.indexOf('bison', 2));
            // Expected output: 4
            console.log(beasts.indexOf('giraffe'));
            // Expected output: -1
        

Category : string

The indexOf() method returns the position of the first occurrence of a value in a string. The indexOf() method returns -1 if the value is not found. The indexOf() method is case sensitive.

Syntax :

        string.indexOf(searchvalue, start)
        

Parameters:

searchvalue : required; the string to search for.

start : optional. The position to start from (default is 0).

Examples: document.append()

            <p>indexOf() returns the position of the first occurrence of a value in a string.</p>
            <p>Find "welcome":</p>
            <p id="demo"></p>
            <script>
                let text = "Hello world, welcome to the universe.";
                let result = text.indexOf("welcome");
                document.getElementById("demo").innerHTML = result;
            </script>
        

Practical examples

example: using indexOf()

example: using indexOf()

code:
                    <div>
                        <p id="index-A"></p>
                        <p id="index-1"></p>
                        <p id="index-2"></p>
                        <p id="index-3"></p>
                        <p id="index-4"></p>
                        <p id="index-5"></p>
                        <p id="index-6"></p>
                    </div>
                    <script>
                    const array = [2, 9, 9];
                    console.log(array.indexOf(2)); // 0
                    console.log(array.indexOf(7)); // -1
                    console.log(array.indexOf(7)); // -1
                    console.log(array.indexOf(9, 2)); // 2
                    console.log(array.indexOf(2, -1)); // -1
                    console.log(array.indexOf(2, -3)); // 0
                    document.getElementById("index-A").innerHTML = "array : " + array;
                    document.getElementById("index-1").innerHTML = "index of 2 : " + array.indexOf(2);
                    document.getElementById("index-2").innerHTML = "index of 7 : " +array.indexOf(7);
                    document.getElementById("index-3").innerHTML = "index of -7 : " + array.indexOf(-7);
                    document.getElementById("index-4").innerHTML = "index of 9, starting 2 : " + array.indexOf(9, 2);
                    document.getElementById("index-5").innerHTML = "index of 9, starting -1 : " + array.indexOf(2, -1);
                    document.getElementById("index-6").innerHTML = "index of 9, starting -3 : " + array.indexOf(2, -3);
                    </script>
                

example: various usages of indexOf() method.

example: various usages of indexOf()

indexOf() returns the position of the first occurrence of a value in a string:

find "Welcome" :

find "e" :

Find "e", starting at position 5 :

Find "a" :

code:
                    <div>          
                        <p id="index-7"></p>
                        <p class="spec">indexOf() returns the position of the first occurrence of a value in a string:</p>
                        <p class="spec">find "Welcome" : <span style="color: red" id="index-8"></span></p>
                        <p class="spec">find "e" : <span style="color: red" id="index-9"></span></p>
                        <p class="spec">Find "e", starting at position 5 : <span style="color: red" id="index-10"></span></p>
                        <p class="spec">Find "a" : <span style="color: red" id="index-11"></span></p>
                    /div>
                    <script>
                        let text = "Hello world, welcome to the universe.";
                        document.getElementById("index-7").innerHTML = text;
                        let result = text.indexOf("Welcome");
                        document.getElementById("index-8").innerHTML = result;
                        document.getElementById("index-9").innerHTML = text.indexOf("e");
                        document.getElementById("index-10").innerHTML = text.indexOf("e", 5);
                        document.getElementById("index-11").innerHTML = text.indexOf("a", 5);
                    </script>