Reversing an Array with Javascript

javascript
Published on June 21, 2019

Reversing an Array by Modifying It

The reverse() method can be used to reverse the order of elements in an array.

The original array is also changed.

var arr = [1, 2, 3, 4, 5];

arr.reverse();

// [5, 4, 3, 2, 1]
console.log(arr);

The return value of reverse() method is the reversed array.

Reversing an Array without Modification

To prevent modification of the oroginal array we will create a new copy of the array using the slice() method and then reverse it.

var arr = [1, 2, 3, 4, 5];

var reversed = arr.slice().reverse();

// [5, 4, 3, 2, 1]
console.log(reversed);
In this Tutorial