JavaScriptArrays

Array.prototype.reverse()

Reverses the order of the array’s elements in place.

Returns
The same, reversed array.
Changes the original
Yes
On this page

Example

#
JavaScript
const levels = [1, 2, 3, 4]
levels.reverse()

console.log(levels)
console.log('kayak'.split('').reverse().join(''))
Console
[4, 3, 2, 1]
kayak

Definition and usage

#

The reverse() method flips the order of the elements: the first becomes the last, and the last becomes the first. It changes the original array and returns a reference to it.

It’s handy, for example, for showing the newest entries at the top when the data arrives in chronological order.

The non-mutating version is toReversed(). In older code you’ll also see [...array].reverse().

Syntax

#
Syntax
array.reverse()

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Watch out: const reversed = items.reverse() also reverses items, because it’s the same array.
  • To reverse a string, chain split(""), reverse(), and join("").

Related entries

#

Spotted a mistake or missing an example? Write to us.