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
Fundamentals
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.
Concept
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.
Foundation
📝 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.
Cheat Sheet
⚡ Quick Reference
Goal
Code
Last index of value
arr.lastIndexOf(value)
Search backward from index
arr.lastIndexOf(value, fromIndex)
Not found
-1
First index instead
arr.indexOf(value)
Mutates array?
No
Compare
📋 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
Hands-On
Examples Gallery
Open DevTools Console (F12) or use Try-it labs. Example N maps to ?tryit=N.
Only the trailing 2 at index 6 is removed. Earlier duplicates at 1 and 4 remain.
Applications
🚀 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.
Important
📝 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+.
Compatibility
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 ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
Array.lastIndexOf()Excellent
Bottom line: Safe to use everywhere except very old IE8 environments. No polyfill needed for modern projects.
Wrap Up
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.
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
Summary
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
📝01
Syntax
arr.lastIndexOf(val)
API
📍02
Last index
Right to left.
Direction
−103
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.