Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท Array

Array.prototype.toReversed()

Reference for JavaScript Array.prototype.toReversed(): get a reversed copy without changing the original array, how it handles holes, and how it differs from reverse().

The toReversed() method returns a new array with the elements in reverse order. The original array is left untouched. It is the non-mutating version of reverse().

It removes the need for the [...array].reverse() copy idiom and is handy when you work with state that must not be modified in place.

JavaScript
const original = [1, 2, 3];
const flipped = original.toReversed(); // [3, 2, 1]
// original is still [1, 2, 3]

Try it

JS A copy, not a change

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Holes and array-likes

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
array.toReversed()

Parameters: none.

Return value: a new array containing the same elements in reverse order.

Exceptions: none for ordinary arrays.

Usage notes

  • Non-mutating. The original array keeps its order, and the result is a different array. reverse() changes the original and returns it.
  • Shallow copy. Objects inside are shared between the original and the copy, so mutating one object is visible in both.
  • Holes become undefined. Unlike reverse(), which preserves holes, toReversed() always produces a dense array. In the example above the hole is turned into an actual undefined element.
  • Where it helps. In UI code that treats state as immutable, or when you want to read an array backward without disturbing other code that holds a reference to it.
  • Pairs with toSorted(), toSpliced() and with(), the other copying counterparts of mutating methods.
  • Generic. It can be called on array-likes and returns a real array.
  • Old browsers: a fallback is array.slice().reverse() or [...array].reverse().
  • Just iterating backward? A reverse for loop avoids allocating a copy.

Browser support

Array.prototype.toReversed() is a recent addition (ES2023) available in current browsers, but not in older ones, so check a compatibility table if you support them.