To reverse an array in JavaScript, do this:
[ ...array ].reverse()
.Here's how you do it:
const array = [ 1, 2, 3, 4, 5 ];
const reversedArray = [ ...array ].reverse();
console.log(reversedArray); // [ 5, 4, 3, 2, 1 ]
[ ...array ]
is important, since reverse
modifies the original array, thus we must first copy the original array to not affect it.