Latest Code Tutorials

Javascript Array lastIndexOf: How to Search Specific Element

To find a particular character’s position in the array then Javascript lastIndexOf() function is useful for you. If you want to search an array from the start to end, use the Javascript array indexOf() method.

Javascript Array lastIndexOf()

Javascript Array lastIndexOf is an inbuilt function that searches an array for the specified element and returns its position. The search will start backward at the specified position, or the end if no start position is specified, and end the search at the beginning of the array. It returns -1 if an element is not found.

The lastIndexOf compares searchElement to elements of the Array using strict equality (the same method used by the ===, or triple-equals, operator).


The array lastIndexOf() syntax is following.

array.lastIndexOf(item, index)


The item parameter is required, and it is the item to search for.

The index parameter is optional, and it is the index at which to start searching backward. Defaults to the array’s length, i.e., the whole array will be searched.

If the index is greater than or equal to the length of the array, the whole array will be searched. If the negative argument is passed then, it is taken as the offset from the end of the array.

Return Value

The lastIndexOf() function returns the index of the found element from the last.

See the following code example.

// app.js

let shows = ["GoT", "Chernobyl", "Breaking Bad", "Suits", "Friends"];
let output = shows.lastIndexOf("Suits");

See the output.

Javascript Array lastIndexOf Tutorial

If an element to search for is present more than once, the lastIndexOf method returns the position of the last occurrence.

See the following example.

// app.js

let shows = ["GoT", "Chernobyl", "GoT", "Suits", "Friends", "GoT", "Friends"];
let output = shows.lastIndexOf("Friends", 6);

The length of the show’s array is 6.

Now, in the lastIndexOf() method, we have passed the start index which is 6. So it will start searching backward from array index 6 and if the element is found then its last index will be in the output.

In the above code, we are searching the Friends string from the 6th index, and also the 6th item is Friends so, the output will be 6 of the above example.

JS Array lastIndexOf() Example

#Pass the negative index

Okay, now negative index count from backward means the last element’s index will be -1 and the second last element’s index will be -2 and so on.

See the following example.

// app.js

let shows = ["GoT", "Chernobyl", "GoT", "Suits", "Friends", "GoT", "Friends"];
let output = shows.lastIndexOf("GoT", -4);

In the above code, we are searching from the end of the array. So, -4th element will be “Suits” and we are searching for GoT.

Now, the only 0th and 2nd position got the GoT and the last index of term GoT is 2 counting from left to right which is 2. So the output will be 2.

Pass the negative index

Different from the indexOf method, the lastIndexOf() method searches for the element backward, starting at index.

The above method is the JavaScript extension to the ECMA-262 standard; as such it may not be present on other implementations of the standard. If we want to make it work, we need to add the Polyfills.

Finally,Javascript Array lastIndexOf() is over.

See also

Javascript string indexOf()

Javascript array indexOf()

Javascript string lastIndexOf()

1 Comment
  1. Amit Parmar says

    thanks for the post

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.