DEV Community

Nedy Udombat
Nedy Udombat

Posted on

Understanding JavaScript Array Series XIV - Array Loops & Iteration Part XI

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

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?

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.


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:

// 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?

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?

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.


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:

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

Thank you for reading. 👍

Top comments (0)