JavaScript Array reverse()
The JavaScript Array.reverse()
method can be used to reverse the order of the elements in an array. This method modifies the array in place, meaning it changes the array itself, rather than returning a new array.
Syntax
The syntax for the Array.reverse()
method is as follows:
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
array.reverse()
[/dm_code_snippet]
Where array
is an array.
Example
The following example shows how to use the Array.reverse()
method to reverse the order of the elements in an array:
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
let array = [1, 2, 3, 4, 5];
array.reverse();
// The array is now [5, 4, 3, 2, 1]
[/dm_code_snippet]
As you can see, the Array.reverse()
method modifies the array itself and does not return a new array.
Return Value
The Array.reverse()
method returns the reversed array.
Browser Support
The Array.reverse()
method is supported in all modern browsers, including Chrome, Firefox, Safari, and Edge.