Follow The New Tutorial updates on WhatsApp, Telegram or YouTube.

JavaScript Array findIndex() method

The JavaScript array findIndex() method returns the index of first element of the given array that satisfies the provided function condition.

The findIndex() method executes a function for each array element.

The findIndex() method returns the index (position) of the first element that passes a test.

The findIndex() method returns -1 if no match is found.

In this tutorial, you will learn how to use the Array findIndex() method to find the first element that satisfies a given test.

The findIndex() method does not execute the function for empty array elements.

The findIndex() method does not change the original array.

Syntax

array.findIndex(callbackFunction(currentValue, index, arr), thisValue)
  • callbackFunction– Required. A function to run for each array element. It represents the function that test the condition.
  • currentValue– Required. The current element of array.
  • index – It is optional. The index of current element.
  • arr – It is optional. The array on which findIndex() operated.
  • thisValue– It is optional. The value to use as this while executing callback. Default undefined.

Return Value

  • Returns the index of the first element in the array that satisfies the given function.
  • Returns -1 if none of the elements satisfy the function.

Example

const array1 = [5, 12, 8, 130, 44];

const isLargeNumber = (element) => element > 13;

console.log(array1.findIndex(isLargeNumber));
// expected output: 3