Array.prototype.lastIndexOf(value, fromIndex) returns the index of the last match, searching right to left. If nothing matches, it returns -1. It uses strict equality (===) and does not mutate the array. For the first match, use indexOf().
Remember
array.lastIndexOf(value)
array.lastIndexOf(value, fromIndex)
[10, 20, 30, 20].lastIndexOf(20) → 3 // last match
[10, 20, 30, 20].indexOf(20) → 1 // first match
[10, 20, 30, 20].lastIndexOf(99) → -1 // missing
search direction: right → left
equality: ===
NaN is never found
Syntax
Call array.lastIndexOf. Start with this line.
JavaScript
array.lastIndexOf(value, fromIndex?)
Call
What it does
array.lastIndexOf(value, fromIndex?)
Primary signature.
array.lastIndexOf(...)
Call with the arguments your case needs.
Built into JavaScript. Call lastIndexOf(...) on an array instance — no import required.
Library declarations
JavaScript
array.lastIndexOf(value, fromIndex?)
Parameters
Parameter
Description
value
Required. Value to find (strict ===)
fromIndex
Optional. Where backward search starts (default length - 1)
Return Value
Return Value
Description
*
Index (≥ 0) or -1
JavaScript Examples
Example 1 — Find the Last Index
Get the position of the last match, or -1 when it is missing.
Pick by need. First occurrence → indexOf; last → lastIndexOf.
Compatibility
Browser & Runtime Support
Array.prototype.lastIndexOf() is part of ES5 (2009). It has been available in evergreen browsers and Node.js for a long time.
✓ Baseline · ES5
Array.prototype.lastIndexOf()
Supported in all modern browsers, Internet Explorer 9+, and every current Node.js release.
99%Browser 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 everywhere you are likely to run JavaScript. Prefer findLastIndex() when you need a custom test (including NaN).
Frequently Asked Questions
lastIndexOf() returns the index of the last matching element, 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 then moves toward index 0.
No. Like indexOf(), it uses strict equality and never matches NaN. Use findLastIndex with Number.isNaN, or includes() for membership only.
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+.