The toReversed() method of the Array class in JavaScript returns a new array with the elements in reversed order, without modifying the original array.
The syntax of Array.toReversed() method is:
toReversed()
This toReversed() method of Array returns a new array with the elements in reversed order, without modifying the original array.
Array.toReversed() returns value of type Array
.
In JavaScript, we can use the toReversed() method to create a new array with elements in reversed order.
For example,
const arr = [1, 2, 3, 4, 5];
const reversedArray = arr.toReversed();
console.log(reversedArray);
[5, 4, 3, 2, 1]
We can also use the toReversed() method with an array of strings to create a new array with reversed string elements.
For example,
const strArr = ['apple', 'banana', 'cherry', 'date'];
const reversedStrArray = strArr.toReversed();
console.log(reversedStrArray);
['date', 'cherry', 'banana', 'apple']
In this JavaScript tutorial, we learned about toReversed() method of Array: the syntax and few working examples with output and detailed explanation for each example.