What is the use of Array.findIndex() method in JavaScript?


Array is a data type which can store multiple elements of similar data types. For example, if array is declared as integer data type then it stores one or more elements of the integer data type.

In JavaScript, Arrays are objects and these objects have some inbuilt functions and properties by which one can do the operations faster and easier.

In this tutorial a functionality of array object ‘findIndex()’ is explained and demonstrated with some examples.

The ‘findIndex()’ method in JavaScript arrays will return the first element from the given array with the given constraint being satisfied. This method will return the index of the first element in the given array even if there are two or more numbers. If no element is present with the given constraint or condition then, it returns -1.

Syntax

The syntax of the findIndex() method is −

array.findIndex(function(element, index, array), thisArg)

The syntax given above is called as inline callback function syntax as the function and also the other parameters are called in one line at once.

The method ‘findIndex()’ will take five parameters including the function. They are −

  • function − The function is used to execute on all the elements of the array until the constraint is satisfied and then returned true.

  • element − This is the current element in the array which undergoes the given functionality or the constraint in the given array.

  • index − This is the optional parameter which has the access to index of the current element in the array.

  • array − This is an optional parameter which has the value of the given array object having the current element.

  • thisArg − This is an optional parameter which is used as the value of “this” if passed and takes as “undefined” if it is not passed.

If the element in the given array exists and satisfies the condition then, the index of element is returned or else ‘-1’ is returned.

Example 1

Following example demonstrates the usage of findIndex() method in JavaScript. In here we are trying to retrieve the even numbers in the given array −

function isEven(element) { return element % 2 == 0; } let arr = [7, 8, 1, 3, 4]; let firstEvenNumber = arr.findIndex(isEven); console.log("The given array is:",arr) console.log("The first even numbers index in the given array is:",firstEvenNumber); console.log("The first even number at the returned index is:",arr[firstEvenNumber])

Example 2

Let us see another example of this function in here we are printing the elements of the given array that are greater than 45.

var arr = [11, 23, 36, 156, 58]; function findIndex(element) { return element > 45; } var v = arr.findIndex(findIndex) console.log("The given array is:",arr) console.log("The first numbers index in the given array which satisfies the constraint is:",v) console.log("The first number in the given array at the returned index is:",arr[v])

Example 3

Following example uses the findIndex() in the form of an arrow function −

let employee = ["Abdul", "Badavath", "Yadav", "Taruni"]; let index = employee.findIndex((day) => day === "Taruni"); console.log("The given array is:",employee) console.log("The index of the given element is:",index) console.log("The element at the returned index is:",employee[index])

Updated on: 25-Aug-2022

348 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements