The reverse() method of the Array class in JavaScript reverses the order of the elements of an array in place. This means that the first element becomes the last, and the last element becomes the first.
The syntax of Array.reverse() method is:
reverse()
This reverse() method of Array reverses the order of the elements of an array in place. (First becomes the last, last becomes first.)
Array.reverse() returns value of type Array
.
In JavaScript, we can use the reverse() method to reverse the order of elements in an array.
For example,
const arr = [1, 2, 3, 4, 5];
arr.reverse();
console.log(arr);
[5, 4, 3, 2, 1]
We can also use the reverse() method with an array of strings to reverse their order.
For example,
const strArr = ['apple', 'banana', 'cherry', 'date'];
strArr.reverse();
console.log(strArr);
['date', 'cherry', 'banana', 'apple']
In this JavaScript tutorial, we learned about reverse() method of Array: the syntax and few working examples with output and detailed explanation for each example.