JavaScript Array lastIndexOf() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

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?)
CallWhat 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

ParameterDescription
valueRequired. Value to find (strict ===)
fromIndexOptional. Where backward search starts (default length - 1)

Return Value

Return ValueDescription
*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.

JavaScript
const numbers = [10, 20, 30, 20, 40];

console.log(numbers.lastIndexOf(20));  // 3
console.log(numbers.lastIndexOf(99));  // -1
console.log(numbers.lastIndexOf(10));  // 0  (valid index!)
Try It Yourself

How It Works

  1. Last match. 20 appears at indexes 1 and 3; last wins → 3.
  2. Missing value. 99 is not present → -1.
  3. Index 0 is real. 10 is at the start → 0.
  4. Check with !== -1. Never treat the result as a plain boolean.

Example 2 — Start Earlier with fromIndex

Begin the backward search before the end of the array.

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

console.log(numbers.lastIndexOf(2));     // 3
console.log(numbers.lastIndexOf(2, 2));  // 1
console.log(numbers.lastIndexOf(2, 0));  // -1
Try It Yourself

How It Works

  1. Default search. Starts at the end; last 2 is at index 3.
  2. Start at 2. Skips the 2 at index 3; finds the earlier one at 1.
  3. Start at 0. Only checks index 0 (1) → -1.
  4. Direction stays leftward. fromIndex only changes where you begin.

Example 3 — lastIndexOf vs indexOf

Same value, different direction — first vs last index.

JavaScript
const tags = ["js", "css", "js", "html"];

console.log(tags.indexOf("js"));      // 0
console.log(tags.lastIndexOf("js"));  // 2

console.log(tags.indexOf("go"));      // -1
console.log(tags.lastIndexOf("go"));  // -1
Try It Yourself

How It Works

  1. indexOf. Left → right; first "js" at 0.
  2. lastIndexOf. Right → left; last "js" at 2.
  3. Both miss. Missing values return -1 either way.
  4. Pick by need. First occurrence → indexOf; last → lastIndexOf.

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 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 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+.

Next: map()

Transform every element into 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.

11 people found this page helpful