Underscore.js _.last() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Array

Definition

Returns the last element of an array. Passing n returns the last n elements (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 _.last. Start with this line.

Underscore
_.last(array, [n])
CallWhat it does
_.last(array, [n])Primary signature.
_.last(...)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
_.last(array, [n])

Parameters

ParameterDescription
arrayRequired. Source list
nOptional. How many trailing elements to return

Return Value

Return ValueDescription
*Last element, or undefined if empty
*New array of up to n trailing items

Underscore.js Examples

Example 1 — Official: Last Element

Read the final value without computing an index yourself.

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

How It Works

  1. Source. Five numbers; indexes 0 … 4.
  2. No count. Underscore returns a single element, not an array.
  3. Last slot. Equivalent to arr[arr.length - 1] here → 1.
  4. No mutation. The original array is unchanged.

Example 2 — Last n Elements

Pass a count to get a trailing slice as a new array.

Underscore
_.last([5, 4, 3, 2, 1], 2);
// → [2, 1]

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

How It Works

  1. Count form. n = 2 means “take two from the end.”
  2. Keep order. Result is [2, 1], not [1, 2].
  3. Always an array. Even when n is 1, you get [value].
  4. Oversized n. Asking for more than length returns the whole array.

Example 3 — Empty Array

Empty input is safe — the return type still depends on whether you passed n.

Underscore
_.last([]);
// → undefined

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

How It Works

  1. No elements. There is no last slot to read.
  2. Without n. Returns undefined (same idea as a missing index).
  3. With n. Returns an empty array — a consistent list type.
  4. Guard. Prefer the n form when you always want an array.

Example 4 — _.last vs _.first

Same API shape — start vs end of the list.

Underscore
var arr = [5, 4, 3, 2, 1];

_.first(arr);     // → 5
_.last(arr);      // → 1

_.first(arr, 2);  // → [5, 4]
_.last(arr, 2);   // → [2, 1]
Try It Yourself

How It Works

  1. Single value. first → start; last → end.
  2. With n. Both return arrays of length up to n.
  3. Pair with initial/rest. _.initial drops the last; _.rest drops the first.
  4. Pick by intent. Use the helper that names the end you care about.

Browser & Runtime Support

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

_.last()

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

Frequently Asked Questions

_.last(array) returns the last element. Passing n as _.last(array, n) returns the last n elements as a new array. Official example: _.last([5, 4, 3, 2, 1]) → 1.
Without n, _.last([]) returns undefined. With n, such as _.last([], 2), it returns [].
Yes. _.last([5, 4, 3, 2, 1], 2) is [2, 1] — original order, not reversed.
For one element they match on a non-empty array. _.last also supports n trailing elements and returns undefined for an empty array without computing a bad index.
_.first takes from the start; _.last takes from the end. Both accept an optional count.
No. It returns an element or a new array; the source is unchanged.

Next: _.rest()

Return everything after the first element — also known as tail / drop.

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