Underscore.js _.initial() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Array

Definition

Returns everything but the last entry of the array. Pass n to exclude the last n elements (official Underscore docs).

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

Syntax

Call _.initial. Start with this line.

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

Parameters

ParameterDescription
arrayRequired. Source to copy from the front
nOptional. How many trailing items to exclude (default 1)

Return Value

Return ValueDescription
*New array of remaining leading items

Underscore.js Examples

Example 1 — Official: Drop the Last

Without n, Underscore excludes one trailing element.

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

How It Works

  1. Source. Five numbers; the last is 1.
  2. Default n. Omit the count → drop one trailing item.
  3. Result. Leading four values: [5, 4, 3, 2].
  4. Copy. The original array is unchanged.

Example 2 — Drop the Last n

Pass a count to exclude more than one trailing element.

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

How It Works

  1. Count. n = 2 means drop the last two slots.
  2. Removed. 2 and 1 leave the result.
  3. Kept. [5, 4, 3] — length 5 - 2.
  4. Order. Leading order is preserved.

Example 3 — _.initial vs _.rest

Same array, opposite ends — drop the tail vs drop the head.

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

_.initial(nums);  // → [5, 4, 3, 2]  (drop end)
_.rest(nums);     // → [4, 3, 2, 1]  (drop start)
Try It Yourself

How It Works

  1. initial. Keeps the front; removes the last value.
  2. rest. Keeps the tail; removes the first value.
  3. Pick by intent. Trim options from the end → initial; skip a head arg → rest.
  4. Both. Return new arrays; neither mutates nums.

Example 4 — Empty Array & Bounds

Empty lists and oversized counts stay safe — you get [].

Underscore
_.initial([]);              // → []
_.initial([1], 1);          // → []
_.initial([1, 2, 3], 0);    // → [1, 2, 3]
_.initial([1, 2, 3], 9);    // → []
Try It Yourself

How It Works

  1. Empty. Nothing to keep → [].
  2. Drop all. One item with n = 1 → [].
  3. Zero. Drop nothing → full shallow copy of the values.
  4. Oversized. Asking to drop more than length → [], no throw.

Browser & Runtime Support

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

_.initial()

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

_.initial(array, [n]) returns a new array with everything except the last element. Pass n to exclude the last n elements instead.
When n is omitted, Underscore drops one trailing element — the same as _.initial(array, 1).
An empty array []. Dropping more items than exist also returns [], and never throws.
_.initial drops from the end and keeps the front. _.rest drops from the start and keeps the tail.
_.first takes leading items. _.initial keeps leading items by removing trailing ones. Together they cover opposite ends of the array.
No. It returns a new array; the source is unchanged.

Next: _.last()

Read the last element — or the last n elements as an array.

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