JavaScript Array lastIndexOf()
- Previous Page length
- Next Page map()
- Go Back to the Previous Level JavaScript Array Reference Manual
Definition and Usage
lastIndexOf()
The method searches for the specified item in the array and returns its position.
The search will start from the specified position, and if no starting position is specified, it will start from the end and end at the beginning of the array.
If the item is not found, lastIndexOf()
The method returns -1.
If the item to be searched appears more than once,lastIndexOf()
The method will return the position of the last occurrence.
Tip:If you want to search from the beginning to the end, use indexOf()
Method.
Example
Example 1
Search for the item "Apple" in the array:
var fruits = ["Banana", "Orange", "Apple", "Mango"]; var a = fruits.lastIndexOf("Apple");
Example 2
Search for the item "Apple" in the array:
var fruits = ["Banana", "Orange", "Apple", "Mango", "Banana", "Orange", "Apple"]; var a = fruits.lastIndexOf("Apple");
Example 3
Search for the item "Apple" in the array, starting from position 4:
var fruits = ["Banana", "Orange", "Apple", "Mango", "Banana", "Orange", "Apple"]; var a = fruits.lastIndexOf("Apple", 4);
Syntax
array.lastIndexOf(item, start)
Parameter Value
Parameter | Description |
---|---|
item | Required. The item to be searched. |
start | Optional. The position from where to start the search. Negative values count from the end, then search to the beginning. |
Technical Details
Return Value: | A number indicating the position of the specified item, otherwise -1. |
---|---|
JavaScript Version: | ECMAScript 5 |
Browser Support
The numbers in the table indicate the first browser version that fully supports this method.
All browsers fully support it lastIndexOf()
Method:
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Chrome | IE | Edge | Firefox | Safari | Opera |
Support | 9.0 | Support | Support | Support | Support |
Related Page
Tutorial:JavaScript Array
Tutorial:JavaScript Array Const
Tutorial:JavaScript Array Methods
Tutorial:JavaScript Sort Array
Tutorial:JavaScript Array Iteration
- Previous Page length
- Next Page map()
- Go Back to the Previous Level JavaScript Array Reference Manual