Reverse a JavaScript array with reverse() or keep the original order

items.reverse() reverses a JavaScript array and returns that same array. To keep the original order, use items.slice().reverse() or items.toReversed() where available.

How reverse() changes the array

reverse() takes no arguments. The length stays the same while the first and last positions trade places. Run the snippet in the console to reverse the order and restore it. Other code may observe the first change to a shared array, even if you reverse it again later.

const items = ['A', 'B', 'C', 'D'];
items.reverse();
console.log(items); // ['D', 'C', 'B', 'A']
items.reverse();
console.log(items); // ['A', 'B', 'C', 'D']

Check whether the result is the same array

Storing the result in another variable gives you two references to the same array. In the example, returned === items is true. Updating returned[0] also updates items[0]. See ECMAScript: reverse().

const items = [1, 2, 3];
const returned = items.reverse();
console.log(returned);           // [3, 2, 1]
console.log(returned === items); // true
returned[0] = 9;
console.log(items);              // [9, 2, 1]

Reverse the order while keeping the original

To show a reversed order, create a separate array using slice().reverse() or toReversed(). Both give you an independent ordering. If the elements are objects, both arrays still refer to those same objects.

const original = ['A', 'B', 'C'];
const reversed = original.slice().reverse();
console.log(original); // ['A', 'B', 'C']
console.log(reversed); // ['C', 'B', 'A']
console.log(reversed === original); // false

The resulting structures differ when an array has holes. For an ordinary array, slice().reverse() keeps a hole in its reversed position. toReversed() stores undefined there. Run this snippet separately and compare Object.hasOwn() results. See ECMAScript: toReversed().

const sparse = ['A', , 'C', 'D'];
const legacy = sparse.slice().reverse();
console.log(Object.hasOwn(legacy, 2)); // false

if (typeof sparse.toReversed === 'function') {
  const modern = sparse.toReversed();
  console.log(Object.hasOwn(modern, 2)); // true
  console.log(modern[2]);                // undefined
}

Complete example: compare the source and result

Save the HTML in UTF-8 as array-reverse.html and open it. Click the button that reverses in place. Both source and result show ["D","C","B","A"], and the reference comparison is true.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Compare ways to reverse an array</title>
</head>
<body>
<button id="mutate" type="button">Reverse in place</button>
<button id="copy" type="button">Copy and reverse</button>
<button id="modern" type="button">Create a copy with toReversed()</button>
<button id="reset" type="button">Reset</button>
<p id="support-note" hidden>This environment does not provide toReversed().</p>
<pre id="output" aria-live="polite"></pre>
<script>
const initial = ['A', 'B', 'C', 'D'];
let items = initial.slice();
const output = document.querySelector('#output');

function show(label, result, sameReference) {
  output.textContent = `${label}\n` +
    `Current source: ${JSON.stringify(items)}\n` +
    `Result array: ${JSON.stringify(result)}\n` +
    `Same array: ${sameReference}`;
}

document.querySelector('#mutate').addEventListener('click', () => {
  const result = items.reverse();
  show("Ran reverse()", result, result === items);
});
document.querySelector('#copy').addEventListener('click', () => {
  const result = items.slice().reverse();
  show("Ran slice().reverse()", result, result === items);
});
const modern = document.querySelector('#modern');
modern.disabled = typeof items.toReversed !== 'function';
document.querySelector('#support-note').hidden = !modern.disabled;
if (!modern.disabled) {
  modern.addEventListener('click', () => {
    const result = items.toReversed();
    show("Ran toReversed()", result, result === items);
  });
}

document.querySelector('#reset').addEventListener('click', () => {
  items = initial.slice();
  show("Initial state", items, true);
});
show("Initial state", items, true);
</script>
</body>
</html>

Click Reset and try reversing a copy. The source is ["A","B","C","D"] and only the result appears reversed. The reference comparison is false. The toReversed() button gives the same result. If the method is unavailable, the button is disabled and a message appears.