Do you know about findIndex() method in JS? As we know, arrays are linear data structures that store homogenous items i.e. can not be a combination of integers and characters, or integers or strings. These items are indexed in them. JavaScript also provides the ability to locate the index of any data item we want. In this article, we will look carefully into the findIndex() method in JavaScript, a powerful tool for this use case.
What is the findIndex() Method in JavaScript?
In any programming, finding the index of an element in an array is a common task. The main reason is to locate the position of a specific value within the array so that the developer can then manipulate the element according to that index. This can be crucial when working with large datasets. Also, in situations when we need to validate the existence or absence of an element, it becomes relevant.
The findIndex() is a built-in JavaScript method that is used to search through an array and find the index of the first element that satisfies a given condition. If the element is found, the method returns the index of the element. Otherwise, the method returns -1 which justifies that there’s no element in the array matching the condition of the method.
It is important to remember that the method is not mutated, which means that it doesn’t modify the original array.
The syntax of the method is given below:
array_name.findIndex(function(currValue, index, arr), thisValue);
where,
- array_name: The name of the array on which the search has to be performed.
- function: Function will run for every element of the array
- currValue: This holds the current value
- index: Optional Parameter holding the index of the current value.
- arr: Optional parameter to which the current element belongs
- thisValue: A value passed to the function as this value.
Here’s an example of the same:
//declaring a function function checkMarks(marks){ return marks>90; } //declaring an array let marks=[54,67,97,92,83,74,95]; //using the findIndex let index=marks.findIndex(checkMarks); //printing the index of the first element that passed the condition console.log(index);
Output:
2
In this example, we have made a function to checkMarks which checks whether the marks are greater than 90. We use this function as a parameter in the findIndex method which returns the index as 2 indicating that element 97 is greater than 90. Hence, we have obtained the desired result.
Let us see another example, where the method will return -1:
//declaring a function function checkMarks(marks){ return marks>99; } //declaring an array let marks=[54,67,97,92,83,74,95]; //using the findIndex let index=marks.findIndex(checkMarks); //printing the index of first element that passed the condition console.log(index);
Output:
-1
Here, we are using the checkMarks function with the condition of returning marks greater than 99. Hence, when this function is used as a parameter in the method, it will not return true for any element in the array. Hence, no element satisfies the condition due to which no index can be returned. The method returns -1.
Note that it stops iterating through the array as soon as it finds the first element that satisfies the condition. It does not continue to search for additional matches.
JavaScript findIndex() on Empty Arrays
Let us see how this method performs on the empty arrays:
//declaring a function function checkMarks(marks){ return marks>90; } //declaring an array let marks=[]; //using the findIndex let index=marks.findIndex(checkMarks); //printing the index of the first element that passed the condition console.log(index);
Output:
-1
There are no elements in the array on which we have to perform the function, so no index will be found. Hence, the method returns -1.
What is the alternative to findIndex?
The best alternative to the findIndex() method in JavaScript is the find() method. Both of them have the same purpose but the biggest difference is in what they return. The find method returns the first element in an array that satisfies the provided testing function, while findIndex returns the index of that element.
Now you can move on to learn about how to append elements to JavaScript array.
Conclusion
In this article, we have explored the JavaScript findIndex() method, and how to use it with simple arrays and empty arrays. You can start using this method in your JavaScript code and enhance your array manipulation capabilities today!