Underscore.js _.lastIndexOf() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Array

Definition

Returns the index of the last occurrence of value in an array, or -1 if it is not present (official Underscore docs). Stable Array helper — not deprecated.

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

Syntax

Call _.lastIndexOf. Start with this line.

Underscore
_.lastIndexOf(array, value, [fromIndex])
CallWhat it does
_.lastIndexOf(array, value, [fromIndex])Primary signature.
_.lastIndexOf(...)Call with the arguments your case needs.

Load Underscore with import _ from "underscore" (or the UMD CDN build) so these helpers are available as _.

Library declarations
Underscore
_.lastIndexOf(array, value, [fromIndex])

Parameters

ParameterDescription
arrayRequired. List to search
valueRequired. Value to find (===)
fromIndexOptional. Start index for the backward scan

Return Value

Return ValueDescription
*Last matching index, or -1

Underscore.js Examples

Example 1 — Official: Last Occurrence

Find the last index of a repeated value — same call as the docs.

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

How It Works

  1. Source. Six numbers; 2 appears at indexes 1 and 4.
  2. Scan backward. Underscore walks from the end toward the start.
  3. First hit from the end. Index 4 holds 2 → return 4.
  4. Stop early. It does not keep scanning once the last match is found.

Example 2 — Value Not Found

A missing value returns -1 — useful for membership checks.

Underscore
var fruits = ["apple", "banana", "apple"];

_.lastIndexOf(fruits, "apple");  // → 2
_.lastIndexOf(fruits, "grape");  // → -1
Try It Yourself

How It Works

  1. Last apple. "apple" appears at 0 and 2 → last is 2.
  2. Missing grape. No slot equals "grape" → -1.
  3. Check pattern. Use !== -1 when you only care if it exists.
  4. No throw. A miss is a number, not an error.

Example 3 — Search Backward from an Index

Pass fromIndex to ignore matches after that position.

Underscore
var nums = [1, 2, 1, 3, 1];

_.lastIndexOf(nums, 1);     // → 4  (full scan)
_.lastIndexOf(nums, 1, 2);  // → 2  (start at index 2)
Try It Yourself

How It Works

  1. Full search. Without fromIndex, the last 1 is at index 4.
  2. Cap the start. fromIndex 2 begins at index 2 and walks left.
  3. Skip later matches. Index 4 is ignored because the search never starts there.
  4. Result. The last 1 at or before index 2 is index 2.

Example 4 — First vs Last Match

Compare _.indexOf and _.lastIndexOf on the same list.

Underscore
var letters = ["a", "b", "c", "b", "a"];

_.indexOf(letters, "b");      // → 1  (first)
_.lastIndexOf(letters, "b");  // → 3  (last)
Try It Yourself

How It Works

  1. Same value. "b" appears twice — indexes 1 and 3.
  2. Forward helper. _.indexOf returns the first hit → 1.
  3. Backward helper. _.lastIndexOf returns the last hit → 3.
  4. Pick by intent. Need “most recent” or “final separator”? Prefer lastIndexOf.

Browser & Runtime Support

_.lastIndexOf ships with Underscore.js (1.x). It runs anywhere Underscore runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install underscore.

✓ Stable · Underscore 1.x

_.lastIndexOf()

Available in [email protected] (UMD / ESM). Works in evergreen browsers and current Node LTS when Underscore is loaded.

100% With Underscore 1.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 Underscore 1.x+

Bottom line: Safe for production once Underscore is on the page or in your bundle. Not a native browser API — load Underscore first. Prefer a pinned CDN version (e.g. [email protected]). Native Array.prototype.lastIndexOf is an ES5 baseline alternative without Underscore.

Frequently Asked Questions

_.lastIndexOf(array, value) returns the last index where value appears, searching backward with strict ===. If the value is missing, it returns -1.
_.indexOf scans from the start and returns the first match. _.lastIndexOf scans from the end and returns the last match. Both use ===.
Pass a number as the third argument to start searching backward at that index (same idea as Array.prototype.lastIndexOf).
It returns -1. Check with if (_.lastIndexOf(list, value) !== -1) for membership.
Strict equality (===). So 1 and '1' differ, NaN never matches NaN, and objects match only by reference.
Use _.findLastIndex when you need a predicate (function test), not a fixed value. Use _.lastIndexOf for a plain === value search.

Next: _.sortedIndex()

Find where a value should be inserted to keep an array sorted.

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