Lodash _.slice() method
What you’ll learn
- How
_.slice(array, start, end)selects a half-open range[start, end). - That Lodash leaves the source untouched and returns a new shallow array.
- When slice beats specialized helpers such as _.drop() for arbitrary windows.
- Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Understanding native Array.prototype.slice transfers directly; pair with _.reverse() when you must flip a copy instead of the original.
- You are comfortable with zero-based indexing and exclusive upper bounds.
- You can run snippets in Node or open the Try-it labs in a browser.
Overview
_.slice packages the familiar substring-style windowing rules into Lodash’s collection helpers so array-like arguments behave predictably without manual casting.
Non-destructive
Perfect prelude to mutators: duplicate the span you intend to edit while collaborators keep the pristine reference.
Tail offsets
Negative indexes measure from the end so grabbing suffixes does not require manual length arithmetic.
Shallow window
Elements move into a fresh array, yet nested structures remain aliased—clone deeper when isolation must extend inward.
Syntax
_.slice(array, [start=0], [end=array.length]) - array: source collection (array-like values are coerced).
- start: inclusive beginning index; negatives count backward from length.
- end: exclusive stop index; omitted defaults to length.
- Returns: a new array covering
[start, end).
Inclusive start, exclusive end
Indexes 1 and 2 land in the result; index 3 stops the window.
import slice from "lodash/slice";
const nums = [10, 20, 30, 40];
slice(nums, 1, 3);
// → [20, 30]; nums unchanged Negative indexes
start = -2 keeps the final pair of elements without referencing length.
import slice from "lodash/slice";
const ids = ["a", "b", "c", "d"];
slice(ids, -2);
// → ["c", "d"] Shallow duplicate
Omitting start and end clones the whole dense span—a common defensive copy before mutating pipelines.
import slice from "lodash/slice";
const src = [1, 2, 3];
const copy = slice(src);
copy.push(4);
// src → [1, 2, 3]; copy → [1, 2, 3, 4] 📋 _.slice vs native vs _.take / _.drop
| API | Best when |
|---|---|
_.slice(array, start, end) | You need an arbitrary contiguous window with Lodash array-like handling. |
array.slice(start, end) | You already have a real array and want identical semantics without importing Lodash. |
_.take(array, n) / _.drop(array, n) | You only trim from the front (possibly chaining dropRight variants) instead of both bounds. |
Pitfalls to avoid
Nested sharing
Slice copies slot references, not nested graphs—reach for structured clones or Lodash clone helpers when deep independence matters.
Off-by-one at end
Remember end is excluded; inclusive ranges from specs must add one when translating to slice.
Sparse arrays
Holes can propagate into the result depending on engine details—normalize dense arrays first when feasible.
❓ FAQ
Summary
- Purpose:
_.slice(array, start, end)returns a shallow copy of the half-open range without touching the source. - Paired workflows: clone with slice, then pass the copy to APIs such as _.reverse() when the original order must stay frozen.
- Next: Lodash _.sortedIndex(), _.reverse() (previous), or the array methods hub.
After _.reverse() or _.fill(), _.slice is the usual repair kit: copy a window first, mutate the copy, and leave shared references observing the untouched original order.
6 people found this page helpful
