JavaScript Array lastIndexOf() method is used to search for the last occurrence of a specified element in an array. It returns the index of the element if found, or -1
if the element does not exist in the array. The search is performed in reverse order, starting from the end of the array.

Pre-requisites to Learn
Syntax
arr.lastIndexOf(searchElement, fromIndex);
Parameters
Parameter | Description |
---|---|
searchElement (Required) | The element to search for in the array. |
fromIndex (Optional) | The index to start searching backward from. Defaults to the last index of the array. |
Return Value
The lastIndexOf() method returns the index of the last occurrence of the searchElement in the array.-1
if the searchElement
is not found.
Note:
- Searches Backward: Looks for the element starting from the end of the array.
- Handles Duplicates: Returns the last index of the specified element, even if it appears multiple times.
- Uses Strict Equality (
===
): Compares elements using the strict equality operator, which means5
is not equal to'5'
.
Examples of JavaScript Array lastIndexOf() Method
Example 1: Finding the last occurrence of 2
, which is at index 3
.
let arr = [1, 2, 3, 2, 4]; let lastIndex = arr.lastIndexOf(2); console.log(lastIndex); // Output: 3
Example 2: Start searching backward from index 2
and finds the first occurrence of 2
at index 1
.
let arr = [1, 2, 3, 2, 4]; let lastIndex = arr.lastIndexOf(2, 2); console.log(lastIndex); // Output: 1
Example 3: Element Not Found – If the element is not found, the method returns -1
.
let numbers = [1, 2, 3, 4]; let lastIndex = numbers.lastIndexOf(5); console.log(lastIndex); // Output: -1
Example 4: Identifies the last occurrence of 'banana'
at index 3
.
let arr = ['apple', 'banana', 'cherry', 'banana']; let lastIndex = arr.lastIndexOf('banana'); console.log(lastIndex); // Output: 3
Example 5: A negative fromIndex
counts backward from the end of the array. In this case, -3
corresponds to index 2
.
let arr = [10, 20, 30, 40, 50]; let lastIndex = arr.lastIndexOf(30, -3); console.log(lastIndex); // Output: 2
Example 6: The lastIndexOf() method is case-sensitive, so 'apple'
and 'Apple'
are treated as different elements.
let arr = ['Apple', 'Banana', 'apple']; let lastIndex = arr.lastIndexOf('apple'); console.log(lastIndex); // Output: 2 let caseMismatch = arr.lastIndexOf('Apple'); console.log(caseMismatch); // Output: 0
Example 7: The lastIndexOf() method treats empty slots in sparse arrays as undefined.
let sparseArray = [1, , 3, , 5]; let lastIndex = sparseArray.lastIndexOf(undefined); console.log(lastIndex); // Output: 3
Supported Browsers
Browser | Support |
---|---|
Chrome | 1+ |
Firefox | 1+ |
Safari | 3+ |
Edge | 12+ |
Opera | 4+ |
Internet Explorer | 9+ |
Comparison with Other Methods
Method | Purpose |
---|---|
Array.lastIndexOf() Method | Searches for the last occurrence of an element, starting from the end of the array. |
Array.indexOf() Method | Searches for the first occurrence of an element, starting from the beginning of the array. |
Array.includes() Method | Checks if an array contains a value, but does not return the index. |