Course recommendation:
- Previous page length
- Next page map()
- Go back to the previous level JavaScript Array Referentie Handleiding
JavaScript Array lastIndexOf()
lastIndexOf()
Definition and usage
The method searches for the specified item in the array and returns its position.
The search will start from the specified position, if no starting position is specified, it will start from the end and end at the beginning of the array. lastIndexOf()
If the item is not found, then
The method returns -1.lastIndexOf()
If the item to be searched appears more than once,
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.
Instance
Search for the item "Apple" in the array:
Example 1 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 starting position from where to search. Negative values indicate positions counted from the end, then searching 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 lastIndexOf()
Method:
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Chrome | IE | Edge | Firefox | Safari | Opera |
Support | 9.0 | Support | Support | Support | Support |
Relevant pages
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 Referentie Handleiding