JavaScript Array lastIndexOf()Method
Example
Find the position of the array element "Apple":
var a = fruits.lastIndexOf("Apple");
aOutput:
The output of the above example means that "Apple" is located at the 2nd position in the array.
Try it yourself »Definition and Usage
The lastIndexOf() method returns the position of the last occurrence of a specified element in an array, searching backward from the end of the string.
If the element to be searched does not appear, the method returns -1.
This method searches the array for the specified element item from tail to head. The search starts at the start position in the array, or at the end of the array (when no start parameter is specified). If an item is found, it returns the position of the first occurrence of item when searching from tail to front. Array indexes start at 0.
If the specified element is not found in the array, it returns -1.
Tip:If you want to find the first occurrence position in an array, please use theindexOf() method。
Browser Support
![]()
![]()
![]()
![]()
![]()
All major browsers support the lastIndexOf() property, but Internet Explorer 8 and earlier IE versions do not support this method.
Syntax
Parameter Values
| Parameter | Description |
|---|---|
| item | Required. Specifies the string value to be searched. |
| start | Optional integer parameter. Specifies the position in the string to start the search. Its valid values are 0 to stringObject.length - 1. If this parameter is omitted, the search will start from the last character of the string. |
Return Value
| Type | Description |
|---|---|
| Number | If searchvalue exists before the fromindex position in stringObject, the position of the last occurrence of searchvalue is returned. |
Technical Details
| JavaScript Version: | 1.6 |
|---|
More Examples
Example
Find the position of "Apple" in the array:
var a = fruits.lastIndexOf("Apple");
aOutput:
Try it yourself »
Example
Find the position where the string "Apple" appears starting from the fourth position in the array:
var a = fruits.lastIndexOf("Apple",4);
aResult output:
Try it yourself »
Other Extensions
JavaScript Array Object