Table of Contents
Basics
This method changes the position of elements within the array. The first element goes to the last position and the last element goes to the first position. The method returns the reversed array.
[1, 2, 3, 4, 5].reverse()
// [5, 4, 3, 2, 1]
Syntax
array.reverse()
No Parameters
Returns the reversed array
The method will return the reversed array.
const numbers = [1, 2, 4, 5, 6, 7];
const reversed = numbers.reverse();
// reversed: Array [7, 6, 5, 4, 2, 1]
Take care when using .reverse() as it mutates the calling array
Common Uses and Snippets
Return order of items in array
When an array of elements are returned in a reversed order. The reverse
method will restore order.
Like this article? Follow @codebeast on Twitter