How to get last index of an item in JavaScript? [SOLVED]


JavaScript

Introduction to lastIndexOf() Method

The lastIndexOf() method in JavaScript is used to search a string or array for a specified value and return the position of the last occurrence of that value. This method is similar to the indexOf() method, but instead of returning the first occurrence of the value, it returns the last occurrence.

In this article, we will discuss how to make use of the lastIndexOf() method in JavaScript.x

 

Using the lastIndexOf() method with strings

To use the lastIndexOf() method, you pass in the value you want to search for as an argument. The method will then search the string from the end to the beginning and return the position of the last occurrence of the value. If the value is not found, the method will return -1.

Here is an example of how to use the lastIndexOf() method:

let str = "Hello, world!";
let lastIndex = str.lastIndexOf("l");
console.log(lastIndex);

Output:

10

In this example, we are searching the string "Hello, world!" for the last occurrence of the letter "l". The lastIndexOf() method will return the position of the last "l" in the string, which is at index 10.

If we wanted to search for a string instead of a single character, we can do that as well. For example:

let str = "Hello, world!";
let lastIndex = str.lastIndexOf("world");
console.log(lastIndex);

Output

7

In this case, we are searching for the string "world" and the lastIndexOf() method will return the position of the last occurrence of that string, which is at index 7.

In addition to the value you want to search for, the lastIndexOf() method also accepts an optional second argument that specifies the index at which to start the search. This can be useful if you only want to search a portion of the string. For example:

let str = "Hello, world!";
let lastIndex = str.lastIndexOf("l", 5);
console.log(lastIndex);

Output

3

In this example, we call the lastIndexOf() method on the str string with the arguments "l" and 5. This method searches for the last occurrence of the specified value ("l" in this case) in the string, starting at the specified index (5 in this case). If the value is not found, the method returns -1.

In this case, the last occurrence of "l" in the string starting at index 5 is at index 3, so the lastIndexOf() method returns 3.

 

Using the lastIndexOf() method with arrays

The lastIndexOf() method can also be used with arrays in JavaScript. This method works similarly to how it works with strings, but it searches for the specified value in the array instead of a string.

To use the lastIndexOf() method with an array, you can call it on the array and pass in the value you want to search for as an argument. For example:

let fruits = ['apple', 'banana', 'mango', 'orange', 'apple'];
let index = fruits.lastIndexOf('apple');
console.log(index);

Output

4

In this example, the lastIndexOf() method is called on the fruits array with the argument 'apple'. This searches for the last occurrence of 'apple' in the array, starting from the end of the array. The method returns the index of the last occurrence of 'apple', which is 4.

You can also specify a starting index for the search by passing in a second argument to the lastIndexOf() method. For example:

let fruits = ["apple", "banana", "mango", "orange", "apple"];
let index = fruits.lastIndexOf("apple", 3);
console.log(index);

Output

0

In this example, the lastIndexOf() method is called on the fruits array with the arguments 'apple' and 3. This searches for the last occurrence of 'apple' in the array, starting from index 3 and going toward the beginning of the array. The method returns the index of the last occurrence of 'apple' before index 3, which is 0.

It's important to note that the lastIndexOf() method searches for values using strict equality (===), which means that the type of the value being searched for must match the type of the values in the array. For example:

let values = [1, "2", 3, "4", 5];
let index = values.lastIndexOf(2);
console.log(index);

Output

-1

In this example, the lastIndexOf() method is called on the values array with argument 2. However, there is no element in the array with a value of 2 (the closest is '2', which is a string). As a result, the method returns -1, indicating that the value was not found in the array.

 

Summary

We can use the lastIndexOf() method on arrays and strings to return the positional index of the last occurrence of the value passed to the method. So, if we want to find the last index of a value, the lastIndexOf method is the right tool.

 

References

String.prototype.lastIndexOf() - JavaScript | MDN (mozilla.org)
Array.prototype.lastIndexOf() - JavaScript | MDN (mozilla.org)

 

Olorunfemi Akinlua

Olorunfemi Akinlua

He is boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs user-friendly interfaces to enhance digital experiences across various domains. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment