Understanding JavaScript Array Series XIV - Array Loops & Iteration Part XI
Tags: javascript, webdev, beginners
Published: 2019-10-16 22:09:50

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

{% link https://dev.to/nedyudombat/understanding-javascript-array-series-xiii-array-loops-iteration-part-x-1obm %}

Do you recall this question from the article above?
> What if we want to find the last position of nedy in this array `['soji', 'nedy', 'naza', 'chukwudi', 'lii', 'nedy']`? What do we do 🤔? 

Let's attempt to solve this yeah?
```javascript
const names = ['soji', 'nedy', 'naza', 'chukwudi', 'lii', 'nedy'];
console.log(names.indexOf('nedy')) // 1
console.log(names.lastIndexOf('nedy')) // 5
```
From the solution above we can see that using `Array.lastIndexOf()` method actually gives us the desired answer.
So today, I will be talking about using `Array.lastIndexOf()` to iterate over arrays.

## **Array.lastIndexOf()**
  This method searches the array for the last position(index) of a  specified element, if this element does not exist it returns -1. This is more like the opposite of the `Array.indexOf()`, because it searches for the last index while `Array.indexOf()` searches for the first index. `Array.lastIndexOf()` starts its search from the right(end) of the array to the left of the array, for `Array.indexOf()`, the reverse is the case.

It's syntax is similar to that of the `Array.indexOf()`, save for some differences:
```javascript
// syntax
arr.lastIndexOf(element, startIndex);
```
**[element]:** This is the element that will searched for in the array.

**[startIndex]:** This is the position(index) of the array to begin the search from. If this value is not supplied it defaults to `arr.length - 1`. This index reduces by one every iteration, which means that the array is searched from right to left.

**What happens if the `startIndex` is equal to or greater than the length of the array?**
```javascript
const names = ['soji', 'nedy', 'naza', 'chukwudi', 'lii', 'nedy'];

console.log(names.lastIndexOf('nedy', 8)) // 5
console.log(names.lastIndexOf('nedy', 6)) // 5
```
In this scenario the whole array will be searched.

**What happens when we pass a negative value as the `startIndex`?**
```javascript
const names = ['soji', 'nedy', 'naza', 'chukwudi', 'lii', 'nedy'];

console.log(names.lastIndexOf('nedy', -6)) // -1
console.log(names.lastIndexOf('nedy', -4)) // 1
```
Just like in `Array.indexOf()`, when negative `startIndex` is passed, the method begins it search from `arr.length + (startIndex)`, but here it searches from the index backwards. In the first instance, the search begins at `(6+ (-6))` which is 0. At point we have the item `soji` and searching backwards we no longer have `nedy` so it returns -1. In the second scenario, the search begins at (6 + (-4)) which is 2. At that point the search begins at `naza` backwards, here we can find `nedy` at index 1.



## **Conclusion**
  `Array.lastIndexOf()` should be used when you get the last occurrence of a particular item in an array.

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 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: