reverse()
reverse<
T>(array):T[]
Reverses array so that the first element becomes the last, the second element becomes the second to last, and so on.
Unlike the native Array.reverse() which mutates the original array, this function returns a new array, preserving immutability. See Design Philosophy
For ES2023+, consider using Array.toReversed().
Type Parametersβ
T: Tβ
The type of elements in the array.
Parametersβ
array: readonly T[]β
The array to reverse.
Returns: T[]β
A new array with elements in reverse order.
Sinceβ
2.0.0
Performanceβ
O(n) time and space β creates a shallow copy then reverses in place.
Also known asβ
reverse (Lodash, es-toolkit, Remeda, Ramda, Effect) Β· β (Radashi, Modern Dash, Antfu)
Exampleβ
const numbers = [1, 2, 3, 4, 5];
const reversed = reverse(numbers);
console.log(reversed); // [5, 4, 3, 2, 1]
console.log(numbers); // [1, 2, 3, 4, 5] (original unchanged)
How it works?β
Reverses array order. Unlike native Array.reverse(), returns a new array (immutable).
Use Casesβ
Display newest-first without mutating source πβ
Reverse chronological data for UI display.
Native array.reverse() mutates - this returns a new array safely.
const commits = ["Initial commit", "Add feature", "Fix bug", "Release v1.0"];
const newestFirst = reverse(commits);
// => ["Release v1.0", "Fix bug", "Add feature", "Initial commit"]
console.log(commits[0]);
// => "Initial commit" (original unchanged β
)
Toggle sort direction in data tablesβ
Switch between ascending and descending views. Common pattern for sortable columns.
const prices = [19.99, 29.99, 49.99, 99.99];
const descending = reverse(prices);
// => [99.99, 49.99, 29.99, 19.99]
Process items in LIFO orderβ
Handle stack-like structures without mutation. Useful for undo systems, breadcrumbs, or navigation history.
const breadcrumbs = ["Home", "Products", "Electronics", "Laptops"];
const backPath = reverse(breadcrumbs);
// => ["Laptops", "Electronics", "Products", "Home"]