Lodash _.lastIndexOf() method

Beginner
⏱️ 7 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

What you’ll learn

  • How _.lastIndexOf(array, value, [fromIndex]) returns the last matching index or -1.
  • Why it pairs with _.indexOf() when duplicates exist and you need the tail occurrence.
  • How optional fromIndex bounds the backward scan without slicing the array.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Read _.indexOf() first for SameValueZero rules; optional _.last() refresher on end-of-array indexing.

  • You know that -1 means “not found” and remains truthy in conditionals.
  • You can run snippets in Node or open the Try-it labs in a browser.

Overview

_.lastIndexOf answers “where is the final occurrence of this value?” by scanning from the end. It mirrors Array.prototype.lastIndexOf with the same equality rules as _.indexOf, packaged for consistent Lodash imports.

Backward scan

Duplicates resolve to the rightmost matching index by default.

fromIndex

Clamp the search window when you only care about a suffix of the array.

Non-destructive

The collection is read-only; you receive an index or -1.

Syntax

javascript
_.lastIndexOf(array, value, [fromIndex=array.length-1])
  • array: collection to search (array-like values are coerced).
  • value: the element to locate using SameValueZero comparison.
  • fromIndex: optional index where the backward search begins (defaults to the last occupied index).
  • Returns: the last matching index at or before the effective start, or -1 if not found.
1

Last duplicate wins

When the same primitive appears multiple times, lastIndexOf returns the rightmost index.

javascript
import lastIndexOf from "lodash/lastIndexOf";

lastIndexOf([2, 1, 2, 3, 2], 2);
// 4
Try it Yourself
2

Not found

Missing values yield -1, the same sentinel as indexOf.

javascript
import lastIndexOf from "lodash/lastIndexOf";

lastIndexOf([1, 2, 3], 9);
// -1
Try it Yourself
3

Bound the backward scan

Pass fromIndex to ignore matches after a position—here the final 2 at index 4 is skipped, so the answer is 2.

javascript
import lastIndexOf from "lodash/lastIndexOf";

lastIndexOf([2, 1, 2, 3, 2], 2, 3);
// 2
Try it Yourself

📋 _.lastIndexOf vs Array.prototype.lastIndexOf vs _.indexOf

APIScan directionNote
_.lastIndexOf(array, value, fromIndex)Right to leftSameValueZero; Lodash import style; array-like inputs
Array.prototype.lastIndexOfRight to leftBuilt-in on real arrays; same semantics in modern engines
_.indexOf(array, value, fromIndex)Left to rightReturns the first match instead of the last

Pitfalls to avoid

-1

Truthy sentinel

-1 is truthy; compare with === -1 or use includes before splicing or slicing by index.

Ref

Objects compare by identity

Separate object literals never match; pass the same reference you stored in the array.

Pred

Not a predicate search

For “last element where fn(item) is true,” use _.findLastIndex() instead of trying to encode logic into value.

❓ FAQ

No. It scans backward for a match and returns an index (or -1). The source array is unchanged.
SameValueZero semantics align with Array.prototype.lastIndexOf in modern engines: NaN matches NaN, and +0 matches -0 for the search value.
indexOf returns the first match scanning forward; lastIndexOf returns the last match scanning backward from the effective start index.
It selects where the backward search begins (default: the last index). Lodash normalizes negative values against length, mirroring the native method.
Use import lastIndexOf from "lodash/lastIndexOf" or require("lodash/lastIndexOf") for tree-shaking friendly bundles.

Summary

  • Purpose: _.lastIndexOf(array, value, fromIndex) returns the last index where value matches (SameValueZero), or -1.
  • Pairs with: _.indexOf() for first vs last duplicate handling.
  • Next: Lodash _.nth(), _.last() (earlier in this track), or the array methods hub.
Did you know?

When the match is defined by a predicate instead of one fixed value, use _.findLastIndex() to scan from the end with custom logic.

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