Learn about the Array findIndex method in JavaScript

Find an item in an array with the ‘findIndex’ method in JavaScript.

Image for post
Image for post
Photo by Gratisography from Pexels

The findIndex() method returns the index of the first element of the array if the callback function passed to the findIndex method returns true, otherwise returns -1

Syntax

  • callback function → A function to execute on each value in the array until the function returns true, indicating that the satisfying element was found.
  • thisArg → Optional object to use as this when executing callback.

The callback function takes three arguments

  • element → current processing element
  • index (Optional)→ index of the element processing
  • array (Optional)→ array in which findIndex method called.

Note

  • once the callback returns the index, it will not process the remaining elements.

Unlike other array methods such as Array.some(), callback is run even for indexes with unassigned values.

Extras :

The Array.find() method, which returns the value of an array element, instead of its index.

You can find the implementation details of findIndex method here.

Articles related to JavaScript.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store