JavaScript Tutorials

JavaScript Array lastIndexOf()
Syntax & Examples

Array.lastIndexOf() method

The lastIndexOf() method of the Array class in JavaScript returns the last (greatest) index at which a given element can be found in the calling array. If the element is not found, it returns -1.


Syntax of Array.lastIndexOf()

There are 2 variations for the syntax of Array.lastIndexOf() method. They are:

1.
lastIndexOf(searchElement)

Parameters

ParameterOptional/RequiredDescription
searchElementrequiredThe element to locate in the array.

This method returns the last (greatest) index at which a given element can be found in the calling array, starting the search at the end of the array.

Returns value of type Number.

2.
lastIndexOf(searchElement, fromIndex)

Parameters

ParameterOptional/RequiredDescription
searchElementrequiredThe element to locate in the array.
fromIndexoptionalThe index at which to start searching backwards. Defaults to the array's length minus one.

This method returns the last (greatest) index at which a given element can be found in the calling array, starting the search at the specified fromIndex.

Returns value of type Number.



✐ Examples

1 Using lastIndexOf() method to find the last occurrence of an element

In JavaScript, we can use the lastIndexOf() method to find the last occurrence of a specified element in an array.

For example,

  1. We define an array variable arr with elements [1, 2, 3, 4, 3, 2, 1].
  2. We use the lastIndexOf() method with the value 2 to find the last occurrence of the element 2.
  3. The result is stored in the variable lastIndexOfTwo.
  4. We log lastIndexOfTwo to the console using console.log() method to see the last index of the element 2.

JavaScript Program

const arr = [1, 2, 3, 4, 3, 2, 1];
const lastIndexOfTwo = arr.lastIndexOf(2);
console.log(lastIndexOfTwo);

Output

5

2 Using lastIndexOf() method to find the last occurrence of an element starting from a specific index

We can use the lastIndexOf() method to find the last occurrence of a specified element in an array, starting the search from a specific index.

For example,

  1. We define an array variable arr with elements [1, 2, 3, 4, 3, 2, 1].
  2. We use the lastIndexOf() method with the value 3 and the fromIndex 4 to find the last occurrence of the element 3 starting from index 4.
  3. The result is stored in the variable lastIndexOfThreeFromIndex4.
  4. We log lastIndexOfThreeFromIndex4 to the console using console.log() method to see the last index of the element 3 starting from index 4.

JavaScript Program

const arr = [1, 2, 3, 4, 3, 2, 1];
const lastIndexOfThreeFromIndex4 = arr.lastIndexOf(3, 4);
console.log(lastIndexOfThreeFromIndex4);

Output

4

3 Using lastIndexOf() method to find the last occurrence of a string in an array

We can use the lastIndexOf() method to find the last occurrence of a specified string in an array.

For example,

  1. We define an array variable strArr with elements ['apple', 'banana', 'cherry', 'apple', 'banana', 'cherry'].
  2. We use the lastIndexOf() method with the value 'banana' to find the last occurrence of the string 'banana'.
  3. The result is stored in the variable lastIndexOfBanana.
  4. We log lastIndexOfBanana to the console using console.log() method to see the last index of the string 'banana'.

JavaScript Program

const strArr = ['apple', 'banana', 'cherry', 'apple', 'banana', 'cherry'];
const lastIndexOfBanana = strArr.lastIndexOf('banana');
console.log(lastIndexOfBanana);

Output

4

Summary

In this JavaScript tutorial, we learned about lastIndexOf() method of Array: the syntax and few working examples with output and detailed explanation for each example.