Skip to content
Latest: Publish JavaScript Packages to NPM Like a Pro!

lastIndexOf() Array Method in JavaScript Explained

Whenever you use lastIndexOf() on an array, the method does the following:

  1. It searches its calling array for the method’s first argument.
  2. It returns the index position of the last match, or -1 if the method found no match.

Array lastIndexOf() method's
tidbit

Use lastIndexOf() to search an array for the index of the last occurrence of a value.

lastIndexOf() accepts two arguments. Here is the syntax:

callingArray.lastIndexOf(valueToFind, startIndex);

A valueToFind is the first argument accepted by the lastIndexOf() method. It defines the value you wish to find in the calling array.

Example 1: Find the index of the last Tuesday

Section titled “Example 1: Find the index of the last Tuesday”
let daysOfTheWeek = [
"Sunday",
"Tuesday",
"Friday",
"Sunday",
"Tuesday",
"Friday",
];
daysOfTheWeek.lastIndexOf("Tuesday");
// The invocation above will return: 4

Try Editing It

Example 2: Find the index of the last Friday

Section titled “Example 2: Find the index of the last Friday”
let daysOfTheWeek = [
"Sunday",
"Tuesday",
"Friday",
"Sunday",
"Tuesday",
"Friday",
];
daysOfTheWeek.lastIndexOf("Friday");
// The invocation above will return: 5

Try Editing It

[1, 3, 5, 7, 9, 1, 3, 5, 7, 9].lastIndexOf(7);
// The invocation above will return: 8

Try Editing It

The startIndex argument is optional. It specifies the index position where you want the computer to start searching for the valueToFind argument.

Keep in mind that lastIndexOf(), by default, searches backward from a startIndex of callingArray.length - 1.

Therefore, if you omit the startIndex argument, the search will begin from the last item in the calling array.

Example 1: Find the index of the last Tuesday from the 6th index position

Section titled “Example 1: Find the index of the last Tuesday from the 6th index position”
let daysOfTheWeek = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Tuesday",
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
daysOfTheWeek.lastIndexOf("Tuesday", 6);
// The invocation above will return: 2

Try Editing It

The snippet above returned 2 because the search started backward from the sixth index position.

However, suppose you omit the startIndex argument. In that case, the computer will search backward from the last item in the calling array (callingArray.length - 1).

Here’s an example:

let daysOfTheWeek = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Tuesday",
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
daysOfTheWeek.lastIndexOf("Tuesday");
// The invocation above will return: 10

Try Editing It

Example 2: Find the index of the last Tuesday from the negative 6th index position

Section titled “Example 2: Find the index of the last Tuesday from the negative 6th index position”

Note that a negative startIndex argument means that the computer should start its index count from the last item in the calling array. But JavaScript will still do a backward (right to left) search.

let daysOfTheWeek = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Tuesday",
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
daysOfTheWeek.lastIndexOf("Tuesday", -6);
// The invocation above will return: 7

Try Editing It

Suppose startIndex is greater than the calling array’s length. In such a case, the computer will default to callingArray.length - 1.

Here’s an example:

let daysOfTheWeek = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Tuesday",
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
daysOfTheWeek.lastIndexOf("Tuesday", 35);
// The invocation above will return: 10

Try Editing It