Lodash _.last() method

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

What you’ll learn

  • How _.last(array) reads the final element without mutating the collection.
  • That an empty array yields undefined, and how array-like length picks the last index.
  • How this compares to array[array.length - 1], optional chaining, and at(-1).
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Optional prior read _.join() or _.head(); you should know that the last index of a length-n array is n - 1.

  • You understand that reading past the end of a list yields undefined in JavaScript.
  • You can run snippets in Node or open the Try-it labs in a browser.

Overview

_.last is the Lodash name for “give me the final value.” It resolves the last occupied index, returns undefined when there is no tail element, and never allocates a new collection. It pairs conceptually with _.head() at the front of the list.

Tail read

One call returns the element reference at the end of a dense array or array-like object.

Empty-safe

Empty input yields undefined, not an exception—same spirit as _.head.

Importable

Use lodash/last next to other focused Lodash utilities in a tree-shaken bundle.

Syntax

javascript
_.last(array)
  • array: collection to inspect (array-like values are coerced).
  • Returns: the element at the last index, or undefined when the collection is empty or has no occupied last slot.
1

Non-empty array

The last element is returned by reference; mutating it still affects the original array slot.

javascript
import last from "lodash/last";

last(["alpha", "beta", "gamma"]);
// "gamma"
Try it Yourself
2

Empty array

There is no last index when length is 0, so Lodash returns undefined. Combine with nullish coalescing when you need a default.

javascript
import last from "lodash/last";

last([]);
// undefined

last([]) ?? "fallback";
// "fallback"
Try it Yourself
3

Array-like object

Numeric keys plus length behave like an array; the last slot is index length - 1.

javascript
import last from "lodash/last";

last({ 0: "north", 1: "east", length: 2 });
// "east"
Try it Yourself

📋 _.last vs [length - 1] vs at(-1)

APIStrengthNote
_.last(array)Named intent, array-like supportMatches other Lodash imports in a utility module
array[array.length - 1]Zero overhead native syntaxGuard length when the array might be empty
array.at(-1)Relative indexing from the endES2022; reads the same final element on dense arrays

Pitfalls to avoid

Sparse

Hole at the last index

A sparse array may have length > 0 while the final slot is empty; last can still yield undefined even though the array is not logically “empty.”

0

Falsy last elements

0, "", and false are valid tail values; do not treat them as missing when branching.

Pop

Not pop

last does not remove the element. Use pop, slice(0, -1), or _.initial() when you need a shorter array.

❓ FAQ

No. It only reads the resolved last index (length minus one for ordinary arrays) and returns that element reference.
It returns undefined. Lodash does not throw when the collection has no last slot.
For a non-empty dense array they agree on the final element. nth is built for arbitrary indices (including other negative offsets) and a slightly different argument shape.
Lodash documents _.last as the primary name for the tail element; there is no separate exported alias mirroring the head/first pair.
Use import last from "lodash/last" or require("lodash/last") for tree-shaking friendly bundles.

Summary

Did you know?

Use _.initial() when you need every element except the last; _.last only reads the final slot.

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