Reverse an array in place
Mutate an existing array when reverse order is needed.
JavaScript · Function
Array.prototype.reverse() reverses an array in place and returns the same array reference. Use toReversed() or copy the array first when the original must remain unchanged.
Calling reverse() swaps elements from the ends toward the center, changes the original array, and returns that same array reference. It works with array-like objects that provide a length and integer-keyed properties, and it preserves sparse-array empty slots.
reverse()A reference to the original array, with its elements reversed.
const items = [1, 2, 3];
const result = items.reverse();
console.log(items); // [3, 2, 1]
console.log(result === items); // trueReverses the array in place and confirms that the returned value is the same array object.
Mutate an existing array when reverse order is needed.
Create a reversed result without changing source data.
Reverse array-like objects that expose length and integer-keyed properties.
Array.prototype.toReversed()Array.from()Spread syntax (...)