Lodash _.lastIndexOf() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Array

Definition

Like _.indexOf, but it iterates from right to left. Returns the index of the last match, or -1 if none. Uses SameValueZero equality. Part of Lodash 4.x Array.

Pass the required arguments shown below. Lodash Array helpers usually return a new value — check whether the source array is mutated.

Syntax

Call _.lastIndexOf on an array. Start with this line.

Lodash
_.lastIndexOf(array, value, [fromIndex=array.length-1])
CallWhat it does
_.lastIndexOf(array, value, [fromIndex=array.length-1])Primary signature.
_.lastIndexOf(array)Call with an array argument.

Prefer import lastIndexOf from "lodash/lastIndexOf" when you want a smaller bundle.

Library declarations
Lodash
_.lastIndexOf(array, value, [fromIndex=array.length-1])

Parameters

ParameterDescription
arrayRequired. Collection to search
valueRequired. Value to match
fromIndexOptional. Start of backward scan; default length - 1

Return Value

Return ValueDescription
*Index number, or -1

Lodash Examples

Example 1 — Official: Last Occurrence

Scan from the end until the first match from the right.

Lodash
_.lastIndexOf([1, 2, 1, 2], 2);
// → 3
Try It Yourself

How It Works

  1. Source. Official sample: [1, 2, 1, 2].
  2. Target. Look for 2 from the right end.
  3. Last hit. Index 3 matches — search stops.
  4. Earlier 2. Index 1 is ignored because the rightmost match already won.

Example 2 — Official: Search from fromIndex

Start the backward scan at a chosen index.

Lodash
_.lastIndexOf([1, 2, 1, 2], 2, 2);
// → 1
Try It Yourself

How It Works

  1. Start at 2. Index 3 is never visited.
  2. Slot 2. Value is 1 — no match; continue left.
  3. Hit. Index 1 is 2 → return 1.
  4. Use. Find the previous occurrence before a known position.

Example 3 — Value Not Found

Missing values return -1, not an error.

Lodash
_.lastIndexOf([1, 2, 1, 2], 3);
// → -1
Try It Yourself

How It Works

  1. Target. Look for 3.
  2. Full scan. Every element is checked from the right; none match.
  3. Result. Lodash returns -1.
  4. Guard. Use if (i === -1) before indexing with the result.

Example 4 — Find NaN (SameValueZero)

Lodash can locate NaN; native lastIndexOf cannot.

Lodash
_.lastIndexOf([1, NaN, 2, NaN], NaN);
// → 3

[1, NaN, 2, NaN].lastIndexOf(NaN);
// → -1  (native === fails for NaN)
Try It Yourself

How It Works

  1. SameValueZero. Lodash treats NaN as equal to NaN.
  2. Last hit. Index 3 is found from the right.
  3. Native contrast. Array#lastIndexOf uses ===, so NaN never matches.
  4. Rule. Prefer Lodash when searching for NaN.

Example 5 — _.lastIndexOf vs _.indexOf

Same array, opposite search directions.

Lodash
var arr = [1, 2, 1, 2];

_.indexOf(arr, 2);      // → 1  (first)
_.lastIndexOf(arr, 2);  // → 3  (last)
Try It Yourself

How It Works

  1. Forward. _.indexOf stops at the first 2 (index 1).
  2. Backward. _.lastIndexOf stops at the last 2 (index 3).
  3. Pick by need. First occurrence vs latest occurrence.
  4. Predicate? Use _.findLastIndex for custom match logic.

Browser & Runtime Support

_.lastIndexOf is a long-standing Lodash 4.x Array helper. It runs anywhere Lodash runs — modern browsers (via CDN or bundle) and Node.js / Bun / Deno with npm install lodash.

✓ Stable · Lodash 4.x

_.lastIndexOf()

Available in [email protected] full builds and the lodash/lastIndexOf modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.

100% With Lodash 4.x
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
_.lastIndexOf Lodash 4.x

Bottom line: Safe for production once Lodash 4.x is on the page or in your bundle. Prefer import lastIndexOf from "lodash/lastIndexOf" for smaller builds. For the first match, use _.indexOf.

Frequently Asked Questions

_.lastIndexOf(array, value, fromIndex) returns the index of the last occurrence of value in array using SameValueZero equality, scanning from right to left. If not found, it returns -1.
No. It only scans backward and returns a number. The source array is unchanged.
It selects where the backward search begins (default: array.length - 1). Lodash normalizes negative values against length, similar to the native method.
Yes. Lodash uses SameValueZero, so _.lastIndexOf([NaN], NaN) returns 0. Native Array.prototype.lastIndexOf cannot find NaN.
indexOf returns the first match scanning forward; lastIndexOf returns the last match scanning backward from the effective start index.
Use import lastIndexOf from "lodash/lastIndexOf" or require("lodash/lastIndexOf") for tree-shaking friendly bundles. Full build: import _ from "lodash" then _.lastIndexOf.

Next: _.nth()

Get the element at any index — including negative offsets from the end.

_.nth() 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.

10 people found this page helpful