Course recommendation:

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.

Example

Search for the item "Apple" in the array:

Example 1
var a = fruits.lastIndexOf("Apple");

Try it yourself

Example 2

Search for the item "Apple" in the array:

var fruits = ["Banana", "Orange", "Apple", "Mango", "Banana", "Orange", "Apple"];
var a = fruits.lastIndexOf("Apple");

Try it yourself

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);

Try it yourself

Syntax

array.lastIndexOf(item, start)

Parameter value

Parameter Description
item Required. The item to be searched.
start Optional. The starting position of the search. Negative values indicate positions counted from the end, then searching to the beginning.

Technical details

Return value: A number representing 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

Tutorium:JavaScript-Array

Tutorium:JavaScript-Array-Const

Tutorium:JavaScript-Array-Methode

Tutorium:JavaScript-Array-Sortierung

Tutorium:JavaScript-Array-Iteration