JavaScript Array lastIndexOf()
Example
Search an array for the item "Apple":
const fruits = ["Apple", "Orange", "Apple", "Mango"];
fruits.lastIndexOf("Apple") // Returns 2
Try it Yourself »
More "Try it Yourself" examples below.
Definition and Usage
The lastIndexOf() method returns the last index (position) of a specified value.
The search starts at a specified position (at the end if no start position is specified), and ends the search at the beginning of the array.
lastIndexOf() returns -1 if the item is not found.
If the search value is present more than once, the method returns the position of the last occurence.
Tip: If you want to search from start to end, use
indexOf()
Browser Support
lastIndexOf() is fully supported in all modern browsers:
| Chrome | IE | Edge | Firefox | Safari | Opera |
| Yes | 9.0 | Yes | Yes | Yes | Yes |
Syntax
array.lastIndexOf(item, start)
Parameter Values
| Parameter | Description |
|---|---|
| item | Required. The item to search for |
| start | Optional. Where to start the search. Negative values will start at the given position counting from the end, and search to the beginning |
Technical Details
| Return Value: | A Number, representing the position of the specified item, otherwise -1 |
|---|---|
| JavaScript Version: | ECMAScript 5 |
More Examples
Example
Search an array for the last occurence of "Apple":
const fruits = ["Orange", "Apple", "Mango", "Apple", "Banana", "Apple"];
fruits.lastIndexOf("Apple") // Returns 5
Try it Yourself »
Example
Search an array for the last occurence of "Apple", starting the search at position 4:
const fruits = ["Orange", "Apple", "Mango", "Apple", "Banana", "Apple"];
fruits.lastIndexOf("Apple", 4) // Returns 3
Try it Yourself »

