Lodash _.dropRight() method
What you’ll learn
- How
_.dropRight(array, n)returns a new array without the lastnentries. - Defaults for
n, and edge cases for0, empty arrays, andnbeyond the length. - How
dropRightpairs with_.drop,_.initial, and nativeslice. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Read _.drop() first; optional familiarity with array.slice(0, -1) in native JavaScript.
- You know that negative end indexes in
slicecount from the end of the array. - You can run snippets in Node or open the Try-it labs in a browser.
Overview
_.dropRight answers “give me the list without the last n items.” It mirrors _.drop but trims from the end, keeping leading order intact—ideal for stripping footers, closing tokens, or trailing blanks.
Non-destructive
The source array stays the same; you always receive a new array instance.
Trim suffixes
Drop trailing sentinels, checksum fields, or duplicate closing markers after parsing.
Pairs with initial
_.initial is the fixed “drop one from the right” spelling; dropRight generalizes the count.
Syntax
_.dropRight(array, [n=1]) - array: source collection (array-like values are coerced).
- n: non-negative integer count of elements to remove from the end; defaults to
1when omitted. - Returns: a new array of the remaining leading elements in original order.
Default: drop one from the end
With only the array argument, Lodash removes the last element and returns the leading slice.
import dropRight from "lodash/dropRight";
dropRight([1, 2, 3]);
// [1, 2] Drop several from the right
Pass n to strip multiple trailing values in one call.
import dropRight from "lodash/dropRight";
dropRight([1, 2, 3], 2);
// [1] Zero and “drop everything”
n = 0 keeps the whole sequence (new array). If n is at least the length, the result is empty.
import dropRight from "lodash/dropRight";
dropRight([1, 2, 3], 0);
// [1, 2, 3]
dropRight([1, 2, 3], 5);
// [] 📋 _.dropRight vs _.drop vs initial
| API | What it does | Note |
|---|---|---|
_.dropRight(arr, n) | New array without last n items | Default n is 1 |
_.drop(arr, n) | New array without first n items | Symmetric trim from the opposite side |
_.initial(arr) | Drop exactly one from the end | Same as _.dropRight(arr) for ordinary arrays |
Pitfalls to avoid
Shallow copy only
Elements are not cloned; removed positions still exist on the original array if you keep a reference to it.
Trimming from the left
dropRight never removes leading items. For that, use _.drop or slice with a positive start index.
Need in-place pop
pop and splice mutate the source. dropRight is for immutable-style pipelines.
❓ FAQ
Summary
- Purpose:
_.dropRight(array, n)returns a new array without the lastnelements (defaultn = 1). - Safety: the input array is not mutated.
- Next: Lodash _.dropRightWhile(), _.drop(), or the array methods hub.
With the default n = 1, _.dropRight matches _.initial for ordinary arrays: both return all elements except the last, as a new array.
6 people found this page helpful
