Lodash _.last() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Array

Definition

Gets the last element of array. Empty arrays return undefined. Companion to _.head. 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 _.last on an array. Start with this line.

Lodash
_.last(array)
CallWhat it does
_.last(array)Primary signature.
_.last(array)Call with an array argument.

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

Library declarations
Lodash
_.last(array)

Parameters

ParameterDescription
arrayRequired. Collection to read from

Return Value

Return ValueDescription
*Last element, or undefined

Lodash Examples

Example 1 — Official: Last Element

Read the value at the final index.

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

How It Works

  1. Source. Official sample: [1, 2, 3].
  2. Last slot. Index 2 (length - 1) holds 3.
  3. Return. _.last returns that value.
  4. No change. The array still contains three numbers.

Example 2 — Empty Array

No elements means there is nothing to return.

Lodash
_.last([]);
// → undefined
Try It Yourself

How It Works

  1. Empty source. Length is 0.
  2. No last slot. Lodash returns undefined.
  3. Safe. It does not throw — check before using the value.
  4. Tip. Use a default: _.last(arr) ?? fallback.

Example 3 — Last String in a List

Works the same for any element type — here, three words.

Lodash
_.last(["alpha", "beta", "gamma"]);
// → "gamma"
Try It Yourself

How It Works

  1. List of words. Indexes 0 … 2.
  2. Peek. _.last returns "gamma".
  3. Rest stay. "alpha" and "beta" remain in the array.
  4. Use. Good for stacks, logs, or “show latest item” UI.

Example 4 — Single-Element Array

When length is 1, first and last are the same value.

Lodash
_.last([42]);
// → 42

_.head([42]);
// → 42
Try It Yourself

How It Works

  1. One slot. Index 0 is also length - 1.
  2. Both helpers. _.last and _.head return 42.
  3. Still safe. Empty remains the only case that yields undefined.
  4. No mutation. The array still holds [42].

Example 5 — _.last vs _.head

Opposite ends of the same array.

Lodash
var nums = [10, 20, 30];

_.head(nums);  // → 10
_.last(nums);  // → 30
Try It Yourself

How It Works

  1. Head. Reads index 0 → 10.
  2. Last. Reads index 2 → 30.
  3. Pair. Use both when you need start and end without slicing.
  4. All but last. Prefer _.initial when you need everything except the tail.

Browser & Runtime Support

_.last 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

_.last()

Available in [email protected] full builds and the lodash/last 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
_.last Lodash 4.x

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

Frequently Asked Questions

_.last(array) returns the last element of array. For an empty array it returns undefined. It does not change the array.
No. It only reads the final slot (length minus one for ordinary arrays) and returns that element.
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 supports arbitrary indexes (including other negative offsets); use _.last when you only need the tail element.
_.head reads index 0; _.last reads the final index. Same empty-array behavior (undefined).
Use import last from "lodash/last" or require("lodash/last") for tree-shaking friendly bundles. Full build: import _ from "lodash" then _.last.

Next: _.lastIndexOf()

Find the last index of a value — Lodash’s right-to-left counterpart to _.indexOf.

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