Lodash _.initial() method

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

What you’ll learn

  • How _.initial(array) returns every element except the last in a new array.
  • Why [] and [42] both yield [], and how element references are copied shallowly.
  • When to prefer slice(0, -1) or _.dropRight() instead.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Optional prior read _.indexOf(); you should know that arrays are zero-based and that many Lodash array helpers return new arrays.

  • You are comfortable with slice end indexes and the idea of a “non-mutating” copy.
  • You can run snippets in Node or open the Try-it labs in a browser.

Overview

_.initial is the “drop the last element” helper: you keep the prefix of the array and omit the final slot. It mirrors how you might describe “everything except the footer row” when processing tabular data.

Prefix copy

Length decreases by one when the input has at least one element; otherwise you still get an empty array.

Shallow

Nested objects are not cloned; the new array holds the same references as the original prefix.

Complements last

Combine with _.last when you want to split “body” and “tail” without manual index math.

Syntax

javascript
_.initial(array)
  • array: collection to process (array-like values are supported).
  • Returns: a new array of all elements except the last, or [] when there is no element to keep.
1

Typical list

The last value is removed; order of the remaining items is unchanged.

javascript
import initial from "lodash/initial";

initial([1, 2, 3, 4]);
// [1, 2, 3]
Try it Yourself
2

Empty and single-element arrays

With zero or one element there is no “prefix before the last,” so the result is always [].

javascript
import initial from "lodash/initial";

JSON.stringify({
  emptyInput: initial([]),
  oneElement: initial(["only"])
});
// '{"emptyInput":[],"oneElement":[]}'
Try it Yourself
3

Objects stay shared

initial builds a new array wrapper; the objects inside are the same references as in the source.

javascript
import initial from "lodash/initial";

const a = { n: 1 };
const b = { n: 2 };
const c = { n: 3 };
const rest = initial([a, b, c]);
rest[0] === a && rest[1] === b;
// true
Try it Yourself

📋 _.initial vs native slice vs _.dropRight

APIWhat it doesNote
_.initial(array)All elements except the lastLodash import; array-like inputs
array.slice(0, -1)Same indices for real arraysBuilt-in; no Lodash dependency
_.dropRight(array, 1)Drop one element from the endGeneralizes to n > 1; same idea for trailing removal

Pitfalls to avoid

1

One element feels surprising

initial([x]) is [], not [x]. If you need “all but last unless length is 1,” branch on length first.

Deep

Not a deep clone

Mutating an object still visible through initial’s result changes the “same” object in the original array.

Tail

Do not confuse with tail

_.tail removes the first element. Use the name that matches whether you drop the head or the foot of the list.

❓ FAQ

No. It returns a new array containing every element except the last. The original array is unchanged.
For an empty array it returns []. For a one-element array it returns [] because there is nothing before the sole last element.
_.tail returns all elements after the first (drop the head). _.initial returns all elements before the last (drop the tail).
For ordinary arrays the result matches slice(0, -1). Lodash also accepts array-like values and keeps the same import ergonomics as other helpers.
Use import initial from "lodash/initial" or require("lodash/initial") for tree-shaking friendly bundles.

Summary

Did you know?

Lodash pairs _.initial with _.last: initial drops the tail element; _.last keeps only that element.

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