Lodash _.slice() method

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

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

javascript
_.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).
1

Inclusive start, exclusive end

Indexes 1 and 2 land in the result; index 3 stops the window.

javascript
import slice from "lodash/slice";

const nums = [10, 20, 30, 40];
slice(nums, 1, 3);
// → [20, 30]; nums unchanged
Try it Yourself
2

Negative indexes

start = -2 keeps the final pair of elements without referencing length.

javascript
import slice from "lodash/slice";

const ids = ["a", "b", "c", "d"];
slice(ids, -2);
// → ["c", "d"]
Try it Yourself
3

Shallow duplicate

Omitting start and end clones the whole dense span—a common defensive copy before mutating pipelines.

javascript
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]
Try it Yourself

📋 _.slice vs native vs _.take / _.drop

APIBest 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

Deep

Nested sharing

Slice copies slot references, not nested graphs—reach for structured clones or Lodash clone helpers when deep independence matters.

End

Off-by-one at end

Remember end is excluded; inclusive ranges from specs must add one when translating to slice.

Hole

Sparse arrays

Holes can propagate into the result depending on engine details—normalize dense arrays first when feasible.

❓ FAQ

No. Lodash returns a new array containing the selected range; the source keeps its length and element order.
No. The slice stops before end, matching native slice semantics—use end minus one mentally when translating inclusive ranges.
Negative numbers count backward from length, just like Array.prototype.slice: -1 refers to the last element.
No. Slice is shallow: nested objects or arrays inside the window share references with the original collection.
Use import slice from "lodash/slice" or require("lodash/slice") for tree-shaking friendly bundles.

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.
Did you know?

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.

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