Lodash _.reverse() method
What you’ll learn
- How
_.reverse(array)mirrorsArray.prototype.reversewhile fitting Lodash’s array-like handling. - Why the method mutates and still returns the same reference.
- Patterns for an immutable reversed view without surprising collaborators.
- Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Skim _.remove() or _.pull() if you are still building intuition around Lodash mutators versus immutable helpers.
- You know that reversing changes indexes for every element except the middle slot on odd lengths.
- You can run snippets in Node or open the Try-it labs in a browser.
Overview
_.reverse exists so reversing stays ergonomic inside Lodash pipelines and consistent across array-like inputs: mutate the sequence, return the same reference, and keep chaining fluent.
In place
The collection you pass is the one whose order changes—plan snapshots before calling when undo stacks matter.
Symmetric swap
First meets last, inward until the middle—no allocation for the reversal itself.
Chain friendly
Returning the original reference lets you continue with other Lodash mutators without juggling temporaries.
Syntax
_.reverse(array) - array: collection to reverse (array-like values are coerced).
- Returns: the same
arrayreference after its elements are reversed.
Reverse values
Call reverse once and the original binding reflects the flipped order immediately.
import reverse from "lodash/reverse";
const nums = [1, 2, 3];
reverse(nums);
// nums is now [3, 2, 1] Same reference
The return value aliases the input—handy for chaining, risky if you expected a forked array.
import reverse from "lodash/reverse";
const xs = ["a", "b", "c"];
const out = reverse(xs);
// out === xs → true Keep the original order
Clone shallowly, reverse the clone, and leave upstream consumers untouched.
import reverse from "lodash/reverse";
const original = [10, 20, 30];
const mirror = reverse(original.slice());
// original → [10, 20, 30]; mirror → [30, 20, 10] 📋 _.reverse vs native vs non-mutating copies
| API | Mutates input? | Returns |
|---|---|---|
_.reverse(array) | Yes | Same array reference, reversed |
array.reverse() | Yes | Same array reference (native) |
[...array].reverse() | No (copies first) | New array reversed; original untouched |
array.slice().reverse() | No | New array reversed; original untouched |
Pitfalls to avoid
Hidden aliasing
Exported constants or cached arrays reverse for every importer—clone when ownership should stay local.
Expecting immutability
Histories and Redux-style reducers usually forbid this mutation; copy then reverse or use pure helpers instead.
Accidental re-render churn
Frameworks that detect reference equality may miss in-place reversals—pair with intentional state updates.
❓ FAQ
Summary
- Purpose:
_.reverse(array)flips element order in place and returns the same reference. - Immutable view: shallow-copy first (
slice, spread, orconcat) whenever observers rely on the prior ordering. - Next: Lodash _.slice(), _.remove() (previous), or the array methods hub.
Like _.fill() and _.pull(), _.reverse mutates the backing collection—reach for slice or a spread copy first when another reference still needs the original order.
6 people found this page helpful
