JavaScript Array toReversed() Method

The JavaScript Array.toReversed() method creates a new array with the elements in reverse order without modifying the original array. Unlike the Array.reverse() method, which modifies the original array in place, toReversed() returns a new reversed array, leaving the original array unchanged.

For example, array.toReversed() is useful when you need a reversed copy of an array while keeping the original array intact.

Syntax

The syntax for the Array.toReversed() method is:

plaintext
array.toReversed()

Parameters

The Array.toReversed() method does not accept any parameters.

Return Value

The method returns a new array with the elements in reverse order. The original array is not modified.

Examples of JavaScript Array.toReversed() Method

Example 1: Reversing an Array Without Modifying the Original

You can use toReversed() to reverse an array and get a new array while keeping the original array intact.

javascript
const numbers = [1, 2, 3, 4, 5];
const reversedNumbers = numbers.toReversed();
console.log(reversedNumbers);  // Output: [5, 4, 3, 2, 1]
console.log(numbers);          // Output: [1, 2, 3, 4, 5]

Explanation: The toReversed() method returns a new array with the elements in reverse order, but the original numbers array remains unchanged.

Example 2: Using toReversed() on a String Array

You can use the toReversed() method to reverse the order of a string array.

javascript
const fruits = ['apple', 'banana', 'mango'];
const reversedFruits = fruits.toReversed();
console.log(reversedFruits);  // Output: ['mango', 'banana', 'apple']
console.log(fruits);          // Output: ['apple', 'banana', 'mango']

Explanation: The toReversed() method reverses the order of the elements in the fruits array, creating a new array without affecting the original array.

Example 3: Reversing an Array of Objects

You can reverse an array of objects without modifying the original array.

javascript
const users = [
 { name: 'Alice', age: 25 },
 { name: 'Bob', age: 30 },
 { name: 'Charlie', age: 35 }
];
const reversedUsers = users.toReversed();
console.log(reversedUsers);
// Output: [{ name: 'Charlie', age: 35 }, 
			{ name: 'Bob', age: 30 }, 
			{ name: 'Alice', age: 25 }]
console.log(users);
// Output: [{ name: 'Alice', age: 25 }, 
			{ name: 'Bob', age: 30 }, 
			{ name: 'Charlie', age: 35 }]

Explanation: The toReversed() method reverses the order of the objects in the array but does not modify the original users array.

Example 4: Using toReversed() on Nested Arrays

You can use toReversed() to reverse the outer array of a nested array structure, leaving the inner arrays unchanged.

javascript
const nestedArray = [[1, 2], [3, 4], [5, 6]];
const reversedArray = nestedArray.toReversed();
console.log(reversedArray);  // Output: [[5, 6], [3, 4], [1, 2]]
console.log(nestedArray);    // Output: [[1, 2], [3, 4], [5, 6]]

Explanation: The toReversed() method reverses the order of the outer array, but the elements within each inner array remain unchanged.

Example 5: Using toReversed() on an Empty Array

When the toReversed() method is called on an empty array, it returns an empty array.

javascript
const emptyArray = [];
const reversedEmpty = emptyArray.toReversed();
console.log(reversedEmpty);  // Output: []

Explanation: Since the array is empty, the toReversed() method returns an empty array.

JavaScript

4211

298

Related Articles