YouTip LogoYouTip

Jsref Findlastindex Array

# JavaScript Array findLastIndex() Method The `findLastIndex()` method is a powerful built-in JavaScript array method introduced in ECMAScript 2023 (ES2023). It allows you to search an array from right to left (from the end to the beginning) and returns the index of the first element that satisfies a specified testing function. --- ## Quick Example Find the index of the last element in the array that is greater than 18: ```javascript const ages = [3, 10, 18, 20]; // Find the index of the last element greater than 18 const lastIndex = ages.findLastIndex(checkAge); function checkAge(age) { return age > 18; } console.log(lastIndex); // Output: 3 (the index of the value 20) ``` --- ## Definition and Usage The `findLastIndex()` method iterates through an array in reverse order (from the last element to the first) and executes a provided callback function for each element. * **Returns:** The index of the **last** element in the array that passes the test. If no elements pass the test, it returns `-1`. * **Empty Arrays:** The callback function is not executed for empty arrays. * **Immutability:** This method does not mutate (change) the original array. ### Comparison of Array Search Methods JavaScript provides several methods to search for elements in an array. Choose the one that best fits your use case: | Method | Description | | :--- | :--- | | `indexOf()` | Returns the index of the **first** element that matches a specific value. | | `lastIndexOf()` | Returns the index of the **last** element that matches a specific value. | | `find()` | Returns the value of the **first** element that passes a test function. | | `findIndex()` | Returns the index of the **first** element that passes a test function. | | `findLast()` | Returns the value of the **last** element that passes a test function. | | `findLastIndex()` | Returns the index of the **last** element that passes a test function. | --- ## Syntax ```javascript array.findLastIndex(callbackFn, thisArg) ``` ### Parameters | Parameter | Description | | :--- | :--- | | `callbackFn` | **Required.** A function to execute on each value in the array. It should return a truthy value to indicate a match has been found. | | `└─ currentValue` | **Required.** The value of the current element being processed. | | `└─ index` | *Optional.* The index of the current element being processed. | | `└─ arr` | *Optional.* The array `findLastIndex()` was called upon. | | `thisArg` | *Optional.* A value to use as `this` when executing the `callbackFn`. Defaults to `undefined`. | ### Return Value * **Type:** `Number` * **Description:** The index of the last element in the array that passes the test. Returns `-1` if no matching element is found. --- ## Code Examples ### Example 1: Finding the Last Matching Object in an Array This method is highly useful when working with arrays of objects, such as finding the last transaction or the most recent update. ```javascript const inventory = [ { name: "apples", quantity: 2 }, { name: "bananas", quantity: 0 }, { name: "cherries", quantity: 5 }, { name: "bananas", quantity: 10 } ]; // Find the index of the last item named "bananas" const lastBananaIndex = inventory.findLastIndex(item => item.name === "bananas"); console.log(lastBananaIndex); // Output: 3 ``` ### Example 2: Using Arrow Functions You can write cleaner, more concise code by using ES6 arrow functions as the callback: ```javascript const numbers = [5, 12, 50, 130, 44]; // Find the index of the last number that is less than 100 const lastIndexUnder100 = numbers.findLastIndex(num => num < 100); console.log(lastIndexUnder100); // Output: 4 (index of 44) ``` ### Example 3: When No Match is Found If no elements satisfy the testing function, the method returns `-1`. ```javascript const scores = [10, 20, 30, 40]; // Find the index of the last score greater than 50 const index = scores.findLastIndex(score => score > 50); console.log(index); // Output: -1 ``` --- ## Browser Support `findLastIndex()` is an ECMAScript 2023 (ES2023) feature. It is fully supported in all modern browsers released after mid-2023: | Chrome | Edge | Firefox | Safari | Opera | | :---: | :---: | :---: | :---: | :---: | | Chrome 110 | Edge 110 | Firefox 115 | Safari 16.4 | Opera 96 | | Feb 2023 | Feb 2023 | Jul 2023 | Mar 2023 | May 2023 |
← Jsref Findlast ArrayJsref Create Array β†’