Array.prototype.reverse()
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Thereverse()
method ofArray
instances reverses an arrayin place and returns the reference to the same array, the first array element now becoming the last, and the last array element becoming the first. In other words, elements order in the array will be turned towards the direction opposite to that previously stated.
To reverse the elements in an array without mutating the original array, usetoReversed()
.
Try it
const array1 = ["one", "two", "three"];console.log("array1:", array1);// Expected output: "array1:" Array ["one", "two", "three"]const reversed = array1.reverse();console.log("reversed:", reversed);// Expected output: "reversed:" Array ["three", "two", "one"]// Careful: reverse is destructive -- it changes the original array.console.log("array1:", array1);// Expected output: "array1:" Array ["three", "two", "one"]
Syntax
reverse()
Parameters
None.
Return value
The reference to the original array, now reversed. Note that the array is reversedin place, and no copy is made.
Description
Thereverse()
method transposes the elements of the calling array object inplace, mutating the array, and returning a reference to the array.
Thereverse()
method preserves empty slots. If the source array issparse, the empty slots' corresponding new indices aredeleted and also become empty slots.
Thereverse()
method isgeneric. It only expects thethis
value to have alength
property and integer-keyed properties. Although strings are also array-like, this method is not suitable to be applied on them, as strings are immutable.
Examples
Reversing the elements in an array
The following example creates an arrayitems
, containing three elements, thenreverses the array. The call toreverse()
returns a reference to thereversed arrayitems
.
const items = [1, 2, 3];console.log(items); // [1, 2, 3]items.reverse();console.log(items); // [3, 2, 1]
The reverse() method returns the reference to the same array
Thereverse()
method returns reference to the original array, so mutating the returned array will mutate the original array as well.
const numbers = [3, 2, 4, 1, 5];const reversed = numbers.reverse();// numbers and reversed are both in reversed order [5, 1, 4, 2, 3]reversed[0] = 5;console.log(numbers[0]); // 5
In case you wantreverse()
to not mutate the original array, but return ashallow-copied array like other array methods (e.g.,map()
) do, use thetoReversed()
method. Alternatively, you can do a shallow copy before callingreverse()
, using thespread syntax orArray.from()
.
const numbers = [3, 2, 4, 1, 5];// [...numbers] creates a shallow copy, so reverse() does not mutate the originalconst reverted = [...numbers].reverse();reverted[0] = 5;console.log(numbers[0]); // 3
Using reverse() on sparse arrays
Sparse arrays remain sparse after callingreverse()
. Empty slots are copied over to their respective new indices as empty slots.
console.log([1, , 3].reverse()); // [3, empty, 1]console.log([1, , 3, 4].reverse()); // [4, 3, empty, 1]
Calling reverse() on non-array objects
Thereverse()
method reads thelength
property ofthis
. It then visits each property having an integer key between0
andlength / 2
, and swaps the two corresponding indices on both ends,deleting any destination property for which the source property did not exist.
const arrayLike = { length: 3, unrelated: "foo", 2: 4, 3: 33, // ignored by reverse() since length is 3};console.log(Array.prototype.reverse.call(arrayLike));// { 0: 4, 3: 33, length: 3, unrelated: 'foo' }// The index 2 is deleted because there was no index 0 present originally// The index 3 is unchanged since the length is 3
Specifications
Specification |
---|
ECMAScript® 2026 Language Specification # sec-array.prototype.reverse |