# lastIndexOf() Array Method in JavaScript Explained

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

- It searches its calling array for the method's first argument.
- It returns the index position of the last match, or
`-1`

if the method found no match.

- A calling array is an array on which you used
`lastIndexOf()`

. So, in`bestColorsList.lastIndexOf("white")`

,`bestColorsList`

is the calling array. `lastIndexOf()`

is sometimes written as`Array.prototype.lastIndexOf()`

because it is a method of the`Array`

object's`prototype`

property.

## Syntax of the `lastIndexOf()`

Method

`lastIndexOf()`

accepts two arguments. Here is the syntax:

`callingArray.lastIndexOf(valueToFind, startIndex);`

## Argument 1: `valueToFind`

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`

`let daysOfTheWeek = [`

"Sunday",

"Tuesday",

"Friday",

"Sunday",

"Tuesday",

"Friday",

];

daysOfTheWeek.lastIndexOf("Tuesday");

// The invocation above will return: 4

### 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

### Example 3: Find the index of the last `7`

`[1, 3, 5, 7, 9, 1, 3, 5, 7, 9].lastIndexOf(7);`

// The invocation above will return: 8

## Argument 2: `startIndex`

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

`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

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

## Express Your Love for Coding!

### 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

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

- To find the first occurrence of a string within an array, use
`indexOf()`

. - You can also use
`lastIndexOf()`

on a string.

## Overview

`lastIndexOf()`

searches its calling array for the last occurrence of the method's argument.

**Your support matters: **Buy me a coffee to support CodeSweetly's mission of simplifying coding concepts.