JavaScript Array lastIndexOf() Method

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Last match

What You’ll Learn

The lastIndexOf() method returns the last index where a value appears, searching from the end toward the start. It returns -1 if missing. This tutorial covers syntax, five examples, backward fromIndex, comparison with indexOf(), and practical removal of the last duplicate.

01

Syntax

lastIndexOf(val)

02

Last match

Right to left

03

Not found

-1

04

Strict

===

05

fromIndex

Start backward

06

ES5

Wide support

Introduction

When duplicates exist, indexOf() finds the first match. lastIndexOf() finds the final one—useful for trimming trailing duplicates, parsing paths from the end, or removing the most recent occurrence of a tag or token.

Like indexOf(), it uses strict equality and returns a numeric index or -1. The array is not modified.

Understanding the lastIndexOf() Method

array.lastIndexOf(searchElement, fromIndex?) scans backward from fromIndex (default: last element) toward index 0. The first match encountered in that direction is the last occurrence in the array (within the search range).

Index 0 is valid, so always test with !== -1, not a truthy check on the index.

💡
Beginner Tip

lastIndexOf searches for a single value, not a sub-array pattern. To find sequences of elements, use a loop or string methods on a joined copy.

📝 Syntax

General form of Array.prototype.lastIndexOf:

JavaScript
array.lastIndexOf(searchElement, fromIndex)

Parameters

  • searchElement — value to locate.
  • fromIndex — optional; index to start searching backward (default length - 1).

Return value

  • Index of the last matching element (0 or greater).
  • -1 if no match.
  • Original array unchanged.

Common patterns

  • arr.lastIndexOf(x) — last occurrence index.
  • arr.lastIndexOf(x, i) — search backward from index i.
  • arr.indexOf(x) !== arr.lastIndexOf(x) — has duplicates?
  • arr.splice(arr.lastIndexOf(x), 1) — remove last match.

⚡ Quick Reference

GoalCode
Last index of valuearr.lastIndexOf(value)
Search backward from indexarr.lastIndexOf(value, fromIndex)
Not found-1
First index insteadarr.indexOf(value)
Mutates array?No

📋 lastIndexOf() vs indexOf() vs findLastIndex() vs includes()

Choose direction and return type based on whether you need first, last, or boolean results.

lastIndexOf
last index

Exact value

indexOf
first index

Left to right

findLastIndex
predicate

ES2023

includes
true / false

Any occurrence

Examples Gallery

Open DevTools Console (F12) or use Try-it labs. Example N maps to ?tryit=N.

📚 Getting Started

Find the last occurrence of a value.

Example 1 — Find the Last Occurrence

Locate the final 2 in an array with duplicates.

JavaScript
const numbers = [1, 2, 3, 4, 2, 5, 2];

console.log(numbers.lastIndexOf(2));
// 6
Try It Yourself

How It Works

The value 2 appears at indices 1, 4, and 6. lastIndexOf returns the final index, 6.

Example 2 — lastIndexOf() vs indexOf()

Compare first and last positions when duplicates exist.

JavaScript
const colors = ["red", "blue", "green", "blue", "yellow"];

console.log(colors.indexOf("blue"));
// 1

console.log(colors.lastIndexOf("blue"));
// 3
Try It Yourself

How It Works

When indices differ, the value appears more than once. Equal indices mean a single occurrence.

📈 Practical Patterns

Backward fromIndex, missing values, and removal.

Example 3 — Backward Search with fromIndex

Limit how far back the search goes by passing a starting index.

JavaScript
const numbers = [1, 2, 3, 4, 2, 5, 2];

console.log(numbers.lastIndexOf(2));
// 6 (last "2")

console.log(numbers.lastIndexOf(2, 5));
// 4 (search only up to index 5)
Try It Yourself

How It Works

Starting backward from index 5 ignores the final 2 at index 6, so index 4 is returned.

Example 4 — Returns -1 When Not Found

Handle missing values before using the index.

JavaScript
const numbers = [1, 2, 3, 4, 2, 5, 2];

const index = numbers.lastIndexOf(7);

console.log(index);
// -1

if (index !== -1) {
  console.log("Found at", index);
} else {
  console.log("7 not in array");
}
// 7 not in array
Try It Yourself

How It Works

Same sentinel as indexOf: -1 means no match anywhere in the searched range.

Example 5 — Remove the Last Occurrence with splice

Combine lastIndexOf with splice to drop the final duplicate only.

JavaScript
const numbers = [1, 2, 3, 4, 2, 5, 2];
const target = 2;

const lastIdx = numbers.lastIndexOf(target);

if (lastIdx !== -1) {
  numbers.splice(lastIdx, 1);
}

console.log(numbers);
// [1, 2, 3, 4, 2, 5]
Try It Yourself

How It Works

Only the trailing 2 at index 6 is removed. Earlier duplicates at 1 and 4 remain.

🚀 Common Use Cases

  • Remove last duplicate — splice at lastIndexOf.
  • Path segments — find last slash or delimiter index.
  • Detect duplicates — compare with indexOf.
  • Scoped backward search — limit with fromIndex.
  • Undo stack logic — find most recent matching entry.
  • Tag lists — remove the last instance of a label.

🧠 How lastIndexOf() Runs

1

Resolve fromIndex

Default last index; clamp to array bounds.

Start
2

Scan backward

Compare with strict equality toward index 0.

Reverse
3

Return on match

First hit when walking backward = last in array.

Found
4

Or return -1

No match in the searched range.

📝 Notes

  • Returns last match only—use indexOf for first.
  • Not found → -1 (index 0 is valid).
  • Strict ===; does not find NaN.
  • Searches single values, not sub-array sequences.
  • For custom tests, use findLastIndex() (ES2023).
  • ES5—excellent support including IE9+.

Browser & Runtime Support

Array.prototype.lastIndexOf() has been available since ES5 (2009). It is one of the most widely supported array search methods.

Baseline · ES5

Array.prototype.lastIndexOf()

Supported in Chrome 1+, Firefox 1.5+, Safari 3+, Edge (all versions), IE 9+, and all modern Node.js versions.

99% Universal support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
Array.lastIndexOf() Excellent

Bottom line: Safe to use everywhere except very old IE8 environments. No polyfill needed for modern projects.

Conclusion

lastIndexOf() answers “where is the final occurrence?” by searching from the end. Pair it with splice to remove trailing duplicates, or compare with indexOf to detect repeats.

Next, learn map() to transform each element into a new array.

💡 Best Practices

✅ Do

  • Use for last occurrence when duplicates exist
  • Compare with !== -1 before using the index
  • Combine with splice to remove last match only
  • Use fromIndex to limit backward search range
  • Prefer findLastIndex for predicate searches (modern)

❌ Don’t

  • Expect it to find sub-array patterns
  • Test with if (index)—0 is falsy
  • Assume it finds NaN
  • Use when indexOf (first match) is enough
  • Confuse backward fromIndex with forward search

Key Takeaways

Knowledge Unlocked

Five things to remember about Array.lastIndexOf()

Find the last index of a value by searching right to left.

5
Core concepts
📍 02

Last index

Right to left.

Direction
−1 03

Not found

-1 return.

Sentinel
📐 04

vs indexOf

First vs last.

Pair
05

splice

Remove last.

Pattern

❓ Frequently Asked Questions

lastIndexOf() returns the index of the last matching element in the array, searching from right to left. If no match exists, it returns -1.
indexOf() finds the first occurrence (left to right). lastIndexOf() finds the last occurrence (right to left). Both use strict equality and return -1 when not found.
fromIndex sets where the backward search starts. Default is length - 1 (the last element). The search moves toward index 0 from that starting point.
No. Like indexOf(), lastIndexOf() uses strict equality and never matches NaN. Use findLastIndex with Number.isNaN for NaN searches.
No. lastIndexOf() only reads the array and returns an index or -1. The source array is unchanged.
lastIndexOf() is ES5 (2009). It works in all modern browsers and has been supported in Internet Explorer 9+.
Did you know?

Strings also have lastIndexOf() for finding the last substring position: "banana".lastIndexOf("a") returns 5. Array lastIndexOf compares whole elements only.

Continue to map()

Learn how to transform every array element and collect the results in a new array.

map() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful