JavaScript Array.findIndex() Method

The JavaScript Array.findIndex() method is used to find the index of the first element in an array that satisfies a given condition (provided by a callback function). If no element passes the test, it returns -1. The method stops as soon as it finds a match, making it efficient when you are only interested in the first occurrence of a matching element.

For example, array.findIndex(callback) is useful when you need to locate the position of an element in the array, rather than the element itself.

Syntax

The syntax for the Array.findIndex() method is given below:

plaintext
array.findIndex(callback(element, index, array), thisArg)

Parameters

The Array.findIndex() method accepts two parameters:

  • callback (Required): A function that is executed for each element in the array. It takes the following arguments:
    • element: The current element being processed.
    • index (Optional): The index of the current element.
    • array (Optional): The array being traversed.
  • thisArg (Optional): Value to use as this when executing the callback function. If not provided, undefined is used.

Return Value

The method returns the index of the first element that satisfies the condition. If no element matches, it returns -1.

Examples of JavaScript Array.findIndex() Method

Example 1: Finding the Index of the First Element Greater Than a Given Value

You can use the findIndex() method to find the index of the first element in an array that is greater than a specified value.

javascript
const numbers = [5, 12, 8, 130, 44];
const index = numbers.findIndex(num => num > 10);
console.log(index);  // Output: 1

Explanation: The findIndex() method searches the numbers array and returns the index of the first element that is greater than 10. In this case, the element 12 at index 1 satisfies the condition.

Example 2: Finding the Index of an Object in an Array of Objects

The findIndex() method can also be used to locate the index of an object in an array based on a specific property.

javascript
const users = [
 { id: 1, name: 'Alice' },
 { id: 2, name: 'Bob' },
 { id: 3, name: 'Charlie' }
];
const index = users.findIndex(user => user.id === 2);
console.log(index);  // Output: 1

Explanation: The findIndex() method searches the users array and returns the index of the object where the id property is 2. In this case, the object is located at index 1.

Example 3: Using findIndex() with Index and Array Parameters

You can also use the index and array parameters inside the callback function to make more complex searches.

javascript
const numbers = [4, 9, 16, 25];
const index = numbers.findIndex((num, idx) => num > 10 && idx > 1);
console.log(index);  // Output: 2

Explanation: The findIndex() method looks for the first element that is greater than 10 and whose index is greater than 1. It returns 2 because the element 16 at index 2 meets both conditions.

Example 4: Using findIndex() with thisArg

The findIndex() method allows you to pass a thisArg to be used as the this context inside the callback function.

javascript
const threshold = {
 min: 10
};
const numbers = [5, 12, 8, 130, 44];
const index = numbers.findIndex(function(num) {
 return num > this.min;
}, threshold);
console.log(index);  // Output: 1

Explanation: The thisArg parameter is used to set the this value to the threshold object. The findIndex() method then returns the index of the first element that is greater than the min value (10), which is 12 at index 1.

Example 5: When No Match Is Found

If no elements satisfy the condition, the findIndex() method returns -1.

javascript
const numbers = [1, 2, 3, 4, 5];
const index = numbers.findIndex(num => num > 10);
console.log(index);  // Output: -1

Explanation: Since no elements in the numbers array are greater than 10, the findIndex() method returns -1 to indicate that no match was found.

Example 6: Using findIndex() on an Empty Array

When the findIndex() method is called on an empty array, it always returns -1.

javascript
const emptyArray = [];
const index = emptyArray.findIndex(num => num > 10);
console.log(index);  // Output: -1

Explanation: Since the array is empty, there are no elements to test against the condition, and the method returns -1.

JavaScript

4427

779

Related Articles