Last Index Of


The lastIndexOf() method returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards from the specified fromIndex.

Example 1: Finding the Last Occurrence

// An array with a repeated element.
const items = ['pen', 'pencil', 'pen', 'eraser'];

// The index of the last occurrence of 'pen' is found.
const lastPenIndex = items.lastIndexOf('pen');

// The last index is logged.
console.log(lastPenIndex); // Output: 2

Explanation: This example demonstrates the basic functionality of lastIndexOf() to find the index of the last occurrence of an element.


Example 2: Element Not Found

// An array of fruits.
const fruits = ['apple', 'banana', 'orange'];

// The last index of 'grape' is searched, which is not in the array.
const grapeIndex = fruits.lastIndexOf('grape');

// The result is logged to the console.
console.log(grapeIndex); // Output: -1

Explanation: Similar to indexOf(), lastIndexOf() returns -1 if the element is not found in the array.


Example 3: Searching Backwards from an Index

// An array with multiple occurrences of a number.
const numbers = [2, 5, 9, 2, 8, 2];

// The last index of 2 is searched for, starting the search backwards from index 4.
const indexBeforeLast = numbers.lastIndexOf(2, 4);

// The found index is logged.
console.log(indexBeforeLast); // Output: 3

Explanation: The optional fromIndex argument in lastIndexOf() specifies the index at which to start searching backwards.


Example 4: Finding the Last Index of a String

// An array of strings.
const logs = ['info', 'error', 'info', 'warning', 'info'];

// The last index of 'info' is found.
const lastInfoIndex = logs.lastIndexOf('info');

// The index is logged to the console.
console.log(lastInfoIndex); // Output: 4

Explanation: lastIndexOf() works effectively with string elements to find the last occurrence within the array.


Example 5: Case-Sensitive Last Index Search

// An array of strings with varied casing.
const words = ['Test', 'test', 'TEST', 'test'];

// The last index of 'test' (lowercase) is found.
const lastTestIndex = words.lastIndexOf('test');

// The index is logged.
console.log(lastTestIndex); // Output: 3

Explanation: Just like indexOf(), lastIndexOf() is case-sensitive, distinguishing between elements with different capitalization.


Example 6: Using a Negative fromIndex

// An array of characters.
const chars = ['a', 'b', 'c', 'd', 'a', 'e'];

// Search for 'a' starting backwards from the 2nd to last element.
const lastAFromNegativeIndex = chars.lastIndexOf('a', -2);

// The index is logged.
console.log(lastAFromNegativeIndex); // Output: 4

Explanation: A negative fromIndex is treated as an offset from the end of the array. The search still proceeds backwards from that point.


Example 7: Comparing indexOf and lastIndexOf

// An array with a repeated value.
const data = [1, 5, 3, 5, 9];

const firstFive = data.indexOf(5);
const lastFive = data.lastIndexOf(5);

// The first and last indices of 5 are logged.
console.log(`First occurrence of 5: ${firstFive}`);   // Output: First occurrence of 5: 1
console.log(`Last occurrence of 5: ${lastFive}`);     // Output: Last occurrence of 5: 3

Explanation: This example clearly illustrates the difference between indexOf() and lastIndexOf(), showing how they find the first and last occurrences of an element, respectively.