JavaScript - findLastIndex() method

revision:


Category : array

The findLastIndex() method iterates the array in reverse order and returns the index of the first element that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned.

Syntax :

        findLastIndex(callbackFn)
        findLastIndex(callbackFn, thisArg)
    

Parameters:

callbackFn : a function to execute for each element in the array. It should return a truthy value to indicate a matching element has been found, and a falsy value otherwise. The function is called with the following arguments:

element : the current element being processed in the array.
index : the index of the current element being processed in the array.
array : the array findLastIndex() was called upon.

thisArg : optional. A value to use as "this" when executing callbackFn.

Examples:

            const array1 = [5, 12, 50, 130, 44];
            const isLargeNumber = (element) => element > 45;
            console.log(array1.findLastIndex(isLargeNumber));
            // Expected output: 3
            // Index of element with value: 130
        

Category : string

The lastIndexOf() method returns the index (position) of the last occurrence of a specified value in a string. The lastIndexOf() method searches the string from the end to the beginning. It returns the index from the beginning (position 0). It returns -1 if the value is not found. The lastIndexOf() method is case sensitive.

Syntax :

        string.lastIndexOf(searchvalue, start)
    

Parameters:

searchvalue : required. The string to search for.

start : optional. The position where to start. Default value is string length.

Examples:

            <p id="demo"></p>
            let text = "Hello planet earth, you are a great planet.";
            let result = text.lastIndexOf("planet");
            document.getElementById("demo").innerHTML = result;
        

Practical examples

example: fid the index of the last prime number in an array.

code:
                    <div>
                        <p id="find-1"></p>
                        <p id="find-2"></p>
            
                    </div>
                    <script>
                        function isPrime(element) {
                            if (element % 2 === 0 || element < 2) {
                                return false;
                        }
                        for (let factor = 3; factor <= Math.sqrt(element); factor += 2) {
                            if (element % factor === 0) {
                            return false;
                            }
                        }
                        return true;
                        }
                        console.log([4, 6, 8, 12].findLastIndex(isPrime)); // -1, not found
                        console.log([4, 5, 7, 8, 9, 11, 12].findLastIndex(isPrime)); // 5
                        document.getElementById("find-1").innerHTML = "last Prime in the array? " + [4, 6, 8, 12].findLastIndex(isPrime);
                        document.getElementById("find-2").innerHTML = "last Prime in the array? " + [4, 5, 7, 8, 9, 11, 12].findLastIndex(isPrime);
                    </script>
                

example: search for the last occurence of "planet".

code:
                    <div>
                        <p id="find-3"></p>
                        <p id="find-4"></p>
            
                    </div>
                    <script>
                        let text = "Hello planet earth, you are a great planet.";
                        let result = text.lastIndexOf("Planet");
                        let result1 = text.lastIndexOf("planet");
                        document.getElementById("find-3").innerHTML = "last index of 'Planet' : " + result;
                        document.getElementById("find-4").innerHTML = "last index of 'planet' : " + result1;
                    </script>
                

example: find the last position of "planet", starting at position 29.

code:
                    <div>
                        <p id="find-5"></p>
                        <p id="find-6"></p>
                    </div>
                    <script>
                        let text2 = "Hello planet earth, you are a great planet.";
                        let result2 = text.lastIndexOf("planet", 20);
                        document.getElementById("find-5").innerHTML = "last occurence of 'planet' from position 20 : " + result2;
                    </script>