• JavaScript Video Tutorials

JavaScript - Array lastIndexOf() Method



The JavaScript Array.lastIndexOf() method is used to return the last occurrence index of a specified element within an array (if it is present at least once). If the element is not present in the array, it returns −1.

By default, this method starts the search from the last element to the first element. However, if you pass a starting index, the search begins from that specific index and moves towards the beginning of the array.

In addition to that, if you pass a negative value as the starting index, the search still begins from the end of the array.

Syntax

Following is the syntax of JavaScript Array lastIndexOf() method −

array.lastIndexOf(searchElement, fromIndex);

Parameters

This method accepts two parameters. The same is described below −

  • searchElement − This specifies the element to locate within the array.
  • fromIndex (Optional) − The index to start searching from backwards. By default it is array.length−1. If negative, it counts from the end of the array (but still searches from right to left).

Return value

This method returns the index of the last occurrence of the specified element in the array, or −1 if the element is not found.

Examples

Example 1

In the following example, we are fetching the last occurrence index of "2" in the specified array using the JavaScript Array lastIndexOf() method.

<html>
<body>
   <p id="demo"></p>
   <script>
      const numbers = [2, 5, 6, 2, 7, 9, 6];
      const result = numbers.lastIndexOf(2);
      document.getElementById("demo").innerHTML = result;
   </script>
</body>
</html>

Output

As we can see in the output, the last occurrence index of “2” is 3.

3

Example 2

Here, we are trying to fetch the the last occurrence index of “10” which is not present in the specified array −

<html>
<body>
   <p id="demo"></p>
   <script>
      const numbers = [2, 5, 6, 2, 7, 9, 6];
      const result = numbers.lastIndexOf(10);
      document.getElementById("demo").innerHTML = result;
   </script>
</body>
</html>

Output

As there is no element “10” in the array, the result will be −1.

-1

Example 3

In the below example, we are passing the second argument “3” to the lastIndexOf() method. It searches for the element "6" starting from index 3 backward in the array numbers −

<html>
<body>
   <p id="demo"></p>
   <script>
      const numbers = [2, 5, 6, 2, 7, 9, 6];
      const result = numbers.lastIndexOf(6, 3);
      document.getElementById("demo").innerHTML = result;
   </script>
</body>
</html>

Output

The result variable will store the index of the last occurrence of "6" before or at index 3, which is 2.

2

Example 4

The lastIndexOf() method starts searching for the element "2" from the second last position of the array −

<html>
<body>
   <p id="demo"></p>
   <script>
      const numbers = [2, 5, 6, 2, 7, 9, 6];
      const result = numbers.lastIndexOf(2, -2);
      document.getElementById("demo").innerHTML = result;
   </script>
</body>
</html>

Output

It finds the last occurrence of "2" at index 3. Therefore, the output is 3.

3
Advertisements