Understanding Javascript Array Series XII - Array Loops & Iteration Part IX
Tags: javascript, webdev, beginners
Published: 2019-10-14 17:46:43

In the previous article, I talked about iterating over arrays using the  `Array.find()` array method. You can check it out below:

{% link https://dev.to/nedyudombat/understanding-javascript-array-series-xi-array-loops-iteration-part-viii-17gp %}

Today, I will talk about using **array.findIndex()** to iterate over arrays.


## Array.findIndex()
  This method is similar to the `Array.find()` in the last article. It is used to find the first element in an array that meets a particular criterion and returns its position(index) in the array.

  The difference between `Array.find()`  and `Array.findIndex()` is that the former returns the first item in the array that meets a specified criterion while the latter returns the postion(index) of that element in the given array.

```javascript
// syntax
arr.findIndex(callback([currentValue], [arrayIndex], [arr]));
```
**[currentValue]:** This is the current item in the array that is being processed. After the procession, the current value becomes the value of the next element in the array.

**[arrayIndex]:** This is the index of the current value in the array. This also changes after the current value has been processed.

**[arr]**: This is the array being iterated over.

**[callback]**: This is basically a function to be performed on each element of the array. It accepts the first three items (currentValue, index, and array) as arguments.


Let's take a look at the examples from yesterday:
* Find the first even number the given array `[1, 3, 5, 6, 7, 8, 9]` and its position:
```javascript
// index     0  1  2  3  4  5  6 
const arr = [1, 3, 5, 6, 7, 8, 9];

// Array.find returns the element
const even = arr.find(num => num%2 === 0);
console.log(even); //6

// Array.findIndex returns the elements position in the array.
const evenIndex = arr.findIndex(num => num%2 === 0);
console.log(evenIndex); //3
```

* Find the position of the first person in this array that is old enough to buy a car(18):
```javascript
  // array
  const playerArr = [
    { name: "Soji", age: 8},
    { name: "Naza", age: 15},
    { name: "Nedy", age: 22},
    { name: "Ezekiel", age: 17},
    { name: "LII", age: 50},
  ]
  
  const firstEligibleCandidate = playerArr.findIndex(player => player.age > 18);
  console.log(firstEligibleCandidate); //2
```


## **Conclusion**
  `Array.findIndex()` is great when you want to get the position of the first element of an array that meets a particular criterion. In a situation where you want to get the element itself, we can use the `Array.find()` method. I wrote about it [here](https://dev.to/nedyudombat/understanding-javascript-array-series-xi-array-loops-iteration-part-viii-17gp).

Got any other instances for the use of the `Array.findIndex()` function? Please do well to share it in the comment section.

That's all for today, tomorrow we will talk about another set of functions used in array Iteration.

Here is the link to the other articles on this Array series written by me:
* [What is an Array?](https://dev.to/nedyudombat/understanding-javascript-array-series-i-what-is-an-array-l1i)
* [Alternate ways of Creating an Array.](https://dev.to/nedyudombat/understanding-javascript-array-series-ii-alternate-ways-of-creating-an-array-26c8)
* [Array Properties](https://dev.to/nedyudombat/understanding-javascript-array-series-iii-array-properties-43co)
* [Array Loops & Iteration Part I] (https://dev.to/nedyudombat/understanding-javascript-array-series-iv-array-loops-iteration-part-i-334j)
* [Array Loops & Iteration Part II] (https://dev.to/nedyudombat/understanding-javascript-array-series-iv-array-loops-iteration-part-ii-115o)
* [Array Loops & Iteration Part III] (https://dev.to/nedyudombat/understanding-javascript-array-series-iv-array-loops-iteration-part-iii-4f38)
* [Array Loops & Iteration Part IV] (https://dev.to/nedyudombat/understanding-javascript-array-series-vii-array-loops-iteration-part-iv-3nb8)
* [Array Loops & Iteration Part V] (https://dev.to/nedyudombat/understanding-javascript-array-series-viii-array-loops-iteration-part-v-5had)
* [Array Loops & Iteration Part VI] (https://dev.to/nedyudombat/understanding-javascript-array-series-ix-array-loops-iteration-part-vi-126)
* [Array Loops & Iteration Part VII `(find())`] (https://dev.to/nedyudombat/understanding-javascript-array-series-x-array-loops-iteration-part-vii-18dc)
* [Array Loops & Iteration Part VIII `(findIndex())`] (https://dev.to/nedyudombat/understanding-javascript-array-series-xi-array-loops-iteration-part-viii-17gp)

Got any question, addition or correction? Please leave a comment.

Thank you for reading. :thumbsup: