The JavaScript Array lastIndexOf() method is used to find the last occurrence of a specified element in an array. It returns the index of the element if found, and -1 if the element is not present. The search is performed from the end of the array to the beginning and is case-sensitive for strings.
For example, array.lastIndexOf(valueToFind) is useful when you need to determine the last position of an element in an array.
Syntax
The syntax for the Array.lastIndexOf() method is:
array.lastIndexOf(valueToFind, fromIndex)
Parameters
The Array.lastIndexOf() method accepts two parameters:
- valueToFind (Required): The element whose last occurrence needs to be found.
- fromIndex (Optional): The index at which to begin the search. The default is the array’s length minus one. If a negative index is provided, the search starts from the end of the array.
Return Value
The last index of the element in the array, if found. -1 if the element is not found.
Examples of JavaScript Array.lastIndexOf() Method
Example 1: Finding the Last Index of an Element
The lastIndexOf() method returns the last occurrence of the specified element in the array.
const fruits = ['apple', 'banana', 'mango', 'banana'];
const index = fruits.lastIndexOf('banana');
console.log(index); // Output: 3
Explanation: The lastIndexOf() method searches for 'banana' from the end of the array and finds its last occurrence at index 3.
Example 2: Element Not Found
If the element is not present in the array, lastIndexOf() returns -1.
const fruits = ['apple', 'banana', 'mango'];
const index = fruits.lastIndexOf('orange');
console.log(index); // Output: -1
Explanation: The method returns -1 because 'orange' is not present in the fruits array.
Example 3: Case-Sensitivity in lastIndexOf()
The lastIndexOf() method is case-sensitive when searching for strings.
const fruits = ['Apple', 'Banana', 'Mango'];
const index = fruits.lastIndexOf('apple');
console.log(index); // Output: -1
Explanation: The method returns -1 because 'apple' (lowercase) does not match 'Apple' (uppercase “A”) in the fruits array.
Example 4: Using lastIndexOf() with Numbers
The lastIndexOf() method works with numbers as well.
const numbers = [10, 20, 30, 40, 30];
const index = numbers.lastIndexOf(30);
console.log(index); // Output: 4
Explanation: The method finds the last occurrence of the number 30 at index 4.
Example 5: Using lastIndexOf() with fromIndex
You can specify a starting index for the search using the fromIndex parameter.
const numbers = [10, 20, 30, 40, 30];
const index = numbers.lastIndexOf(30, 3);
console.log(index); // Output: 2
Explanation: The search begins from index 3, and the last occurrence of 30 before this index is found at index 2.
Example 6: Using lastIndexOf() with Negative fromIndex
You can use a negative fromIndex to start searching from the end of the array.
const numbers = [1, 2, 3, 4, 5];
const index = numbers.lastIndexOf(3, -2);
console.log(index); // Output: 2
Explanation: The fromIndex of -2 starts the search from the second-to-last element. The number 3 is found at index 2.
Example 7: lastIndexOf() and NaN
The lastIndexOf() method cannot detect NaN in an array because NaN !== NaN.
const array = [NaN, 2, 3];
const index = array.lastIndexOf(NaN);
console.log(index); // Output: -1
Explanation: The method returns -1 because NaN cannot be matched using the lastIndexOf() method.