JavaScript lastIndexOf: The Complete Guide

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

JavaScript lastIndexOf

JavaScript lastIndexOf() is a built-in array 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).

Syntax

The array lastIndexOf() syntax is following.

array.lastIndexOf(item, index)

Parameters

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

The index parameter is optional and 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 array’s length, the whole array will be searched. If the negative argument is passed, 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");
console.log(output);

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);
console.log(output);

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, 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 that the output will be 6 of the above example.

JS Array lastIndexOf() Example

Pass the negative index

The negative index count from backward means the last element’s index will be -1, 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);
console.log(output);

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

Now, the only 0th and 2nd positions 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

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

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

That’s it for this tutorial.

See also

JavaScript string indexOf()

JavaScript array indexOf()

JavaScript array findIndex()

1 thought on “JavaScript lastIndexOf: The Complete Guide”

Leave a Comment

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