JavaScript Array reverse()

The reverse() method returns the array in reverse order.

Example


reverse() Syntax

The syntax of the reverse() method is:

arr.reverse()

Here, arr is an array.


reverse() Parameters

The reverse() method does not take any parameters.


reverse() Return Value

  • Returns the array after reversing the order of its elements.

Note: The reverse() method reverses the order of elements in place, it means the method changes the original array.


Example 1: Using reverse() Method

Output

Reversed Array: [ 'Lua', 'Java', 'C++', 'Python', 'JavaScript' ]
Original Array: [ 'Lua', 'Java', 'C++', 'Python', 'JavaScript' ]

In the above example, we have used the reverse() method to reverse the languages array.

languages.reverse() reverses the order of each element in the array and returns the reversed array.

Since the method modifies the original array, both languages and reversedArray hold the same value.


Example 2: reverse() Method with Spread Operator

In Example 1, we saw how the reverse() method modifies the original array.

But if we use the spread operator(...) in the array along with the reverse() method, it does not modify the original array. For example:

Output

Reversed Array: [ 'Lua', 'Java', 'C++', 'Python', 'JavaScript' ]
Original Array: [ 'JavaScript', 'Python', 'C++', 'Java', 'Lua' ]

Recommened Reading: JavaScript Array sort()