Lodash _.take() method

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

What you’ll learn

  • How _.take(array, n) copies the leading prefix without touching the source.
  • That omitting n keeps exactly one element and n = 0 yields [].
  • When _.slice() or predicate-driven helpers replace blind counts.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

_.tail() returns everything after the first slot; take keeps only the opening segment—compose both when splitting pipelines explicitly.

  • You understand shallow copies: nested objects inside the prefix remain aliased.
  • You can open Try-it labs or run snippets locally.

Overview

_.take extracts bounded previews—pagination hints, sample rows for dashboards, or argv slices—without manual index juggling.

Prefix-only

Counts strictly from index zero—pair with takeRight variants when trimming the opposite end.

Safe clamping

Over-large counts degenerate into full shallow clones rather than throwing.

Non-destructive

Consumers downstream keep referencing the untouched tail elements.

Syntax

javascript
_.take(array, [n=1])
  • array: dense collection or array-like value.
  • n: non-negative integer count of leading elements to include (defaults to 1).
  • Returns: new array with at most n slots copied from the start.
1

Grab the opening run

Choose how many leading entries survive—ordering stays identical.

javascript
import take from "lodash/take";

take(["alpha", "beta", "gamma", "delta"], 2);
// → ["alpha", "beta"]
Try it Yourself
2

Zero count vs default

n = 0 produces an empty array; omitting n falls back to capturing only the first element.

javascript
import take from "lodash/take";

take([10, 20, 30, 40], 0);
// → []

take([10, 20, 30, 40]);
// → [10]
Try it Yourself
3

Count larger than length

Lodash clamps to the available span—you clone every element without errors.

javascript
import take from "lodash/take";

take([1, 2, 3], 99);
// → [1, 2, 3]
Try it Yourself

📋 _.take vs _.slice, _.drop, _.takeWhile

APIPrimary knobUse case
_.take(array, n)Fixed leading countKnown prefix widths such as previews
_.slice(array, start, end)Arbitrary half-open windowOffsets not anchored at zero or asymmetric ends
_.drop(array, n)Skip leading cellsInverse framing—keep suffix after skipping
_.takeWhile(array, predicate)Logical cutoffCounts unknown until a predicate fails

Pitfalls to avoid

Deep

Shallow prefix

Nested collections remain referenced—clone deeply only when isolation requires it.

Logic

Blind counts

take ignores element values; validate assumptions before slicing sensitive batches.

Sparse

Sparse arrays

Holes may surface depending on engine behavior—normalize dense arrays first when feasible.

❓ FAQ

No. Lodash returns a new array containing up to n leading elements; the source stays unchanged.
Lodash defaults n to 1, so _.take(array) keeps only the first slot.
You receive every element—a shallow copy of the whole dense span, equivalent to slice from zero through length.
take counts slots unconditionally from the front; takeWhile stops only after a predicate fails.
Use import take from "lodash/take" or require("lodash/take") for tree-shaking friendly bundles.

Summary

Did you know?

_.take mirrors _.drop(): prefix versus suffix trims from the same edge—combine both when framing sliding windows without bespoke slice arithmetic.

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