How to Find the Last Position of a Value in JavaScript

Find the Last Position of a Value in JavaScript

There are times when you don’t just want to know if a value exists in an array—you want to know the last time it appears. This is especially useful when values can repeat, such as in logs, selections, or lists with duplicates. JavaScript offers a simple built-in way to do this, plus a few more customizable options when you need extra control.

In this tutorial, we'll explore how to find the last position (or index) of a value in a JavaScript array. You'll see how to use the built-in lastIndexOf() method, and how to manually find the last match using loops when you need conditional logic. These methods are beginner-friendly and illustrated with simple examples.

Method 1: Using lastIndexOf() – The Easiest Way

The lastIndexOf() method searches the array from the end and returns the index of the last occurrence of a specified value. If the value isn’t found, it returns -1. This method is perfect for arrays with repeated values and works on strings, numbers, and booleans.

const fruits = ["apple", "banana", "cherry", "banana", "date"];

const lastPosition = fruits.lastIndexOf("banana");

console.log("Last position of 'banana':", lastPosition);
Last position of 'banana': 3

Method 2: Using a Reverse for Loop – Full Control

If you want to apply a condition while searching or need more flexibility (like searching objects or checking patterns), you can use a reverse for loop. This method gives you total control over how and what you're searching.

const items = ["Item 1", "Item 2", "Item 3", "Item 2", "Item 4"];
let lastIndex = -1;

for (let i = items.length - 1; i >= 0; i--) {
  if (items[i] === "Item 2") {
    lastIndex = i;
    break;
  }
}

console.log("Last index of 'Item 2':", lastIndex);
Last index of 'Item 2': 3

Method 3: Using findLastIndex() (Modern & Conditional)

As of ECMAScript 2023, JavaScript now includes findLastIndex(), a modern and expressive method that lets you find the index of the last item that meets a condition. This method is excellent for arrays of objects or when applying logical conditions.

const fruits = ["apple", "cherry", "banana", "coconut", "cherry"];

const lastCherryIndex = fruits.findLastIndex(fruit => fruit.startsWith("c"));

console.log("Last index of fruit starting with 'c':", lastCherryIndex);
Last index of fruit starting with 'c': 4

When Should You Use Each Method?

Use lastIndexOf() for quick searches on primitive values when you just need the last match. It’s fast and simple. Reach for a reverse for loop when you need extra logic or want to search objects, patterns, or custom rules. If your environment supports it, findLastIndex() is the cleanest solution for conditional logic and modern syntax.

Final Thoughts

Finding the last position of a value in JavaScript isn’t just about convenience—it’s about accuracy. Especially when values repeat or your logic depends on the most recent match, choosing the right method ensures clarity and correctness.

All three approaches—lastIndexOf(), reverse loops, and findLastIndex()—are beginner-friendly yet powerful. Try them with different types of data and conditions, and you’ll quickly develop the instinct for when and how to use each one. Write code that doesn't just work—but knows exactly where to look.

Comments

💬 Please keep your comment relevant and respectful. Avoid spamming, offensive language, or posting promotional/backlink content.
All comments are subject to moderation before being published.


Loading comments...