The reverse()
method reverses an array in place. The first array element becomes the last, and the last array element becomes the first.
a.reverse()
The reversed array.
The reverse
method transposes the elements of the calling array object in place, mutating the array, and returning a reference to the array.
reverse
is intentionally generic; this method can be called or applied to objects resembling arrays. Objects which do not contain a length
property reflecting the last in a series of consecutive, zero-based numerical properties may not behave in any meaningful manner.
The following example creates an array a
, containing three elements, then reverses the array. The call to reverse()
returns a reference to the reversed array a
.
const a = [1, 2, 3]; console.log(a); // [1, 2, 3] a.reverse(); console.log(a); // [3, 2, 1]
The following example creates an array-like object a
, containing three elements and a length property, then reverses the array-like object. The call to reverse()
returns a reference to the reversed array-like object a
.
const a = {0: 1, 1: 2, 2: 3, length: 3}; console.log(a); // {0: 1, 1: 2, 2: 3, length: 3} Array.prototype.reverse.call(a); //same syntax for using apply() console.log(a); // {0: 3, 1: 2, 2: 1, length: 3}
Specification |
---|
ECMAScript (ECMA-262) The definition of 'Array.prototype.reverse' in that specification. |
Desktop | ||||||
---|---|---|---|---|---|---|
reverse |
1 | 12 | 1 | 5.5 | 4 | 1 |
Mobile | ||||||
---|---|---|---|---|---|---|
reverse |
1 | 18 | 4 | 10.1 | 1 | 1.0 |
Server | |
---|---|
reverse |
0.1.100 |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse