Table of Content
JavaScript toReversed returns a new array with the reversed order of its elements. It does not change the original array at any step. This function works in a predictable and simple way for arrays.
Understand the toReversed Function in JavaScript
The toReversed function creates a copy of an array and puts elements in reverse order. It leaves the original array without any change.
The syntax looks like this:
array.toReversed()It takes no parameters. It returns a new array with elements in reverse order. You write it like this to avoid errors:
const newArray = oldArray.toReversed();It first copies all elements of the array into a new array. It then places the elements from the last to the first index. The original array stays intact and can still be used.
Here is an example:
const numbers = [1, 2, 3];
const reversed = numbers.toReversed();This returns the expected value. The result is [3, 2, 1] and the original array [1, 2, 3] stays the same.
The Difference Between toReversed and reverse
The reverse() method changes the array in place and returns it. The toReversed() method returns a new reversed array without changing the original one.
| Aspect | toReversed | reverse |
|---|---|---|
| Mutates Original | No | Yes |
| Return Value | New reversed array | Same array reversed |
| Use Case | Keep original intact | Change original array |
Use toReversed() when you need a reversed copy without change to the source. Use reverse() when you want to reverse the original array itself.
Examples of the toReversed Function in JavaScript
Reverse Numbers Without Changing Original:
const nums = [10, 20, 30];
const copy = nums.toReversed();This shows it in action clearly. It creates a new array [30, 20, 10] and keeps nums as [10, 20, 30].
Reverse Strings in Array:
const words = ["apple", "banana", "cherry"];
const reversedWords = words.toReversed();This returns the expected value. It outputs ["cherry", "banana", "apple"] and leaves the original array unchanged.
Compare reverse() and toReversed():
const arr = [1, 2, 3];
const rev1 = arr.reverse();
const rev2 = arr.toReversed();This shows it in action clearly. The reverse() method changes arr to [3, 2, 1] while toReversed() keeps arr intact and returns [1, 2, 3] reversed separately.
Chain Methods with toReversed():
const values = [5, 10, 15];
const newVals = values.toReversed().map(x => x * 2);This gives you the result immediately. It first reverses into [15, 10, 5] then maps to [30, 20, 10] without any change to the original array.
Wrapping Up
You learned the meaning of toReversed and the difference from reverse.
Here is a quick recap:
toReversedmakes a reversed copy without change to the source.reversechanges the array itself and returns it.
FAQs
What is JavaScript toReversed Function?
let numbers = [1, 2, 3];
let newArray = numbers.toReversed();
console.log(newArray); // [3, 2, 1]
console.log(numbers); // [1, 2, 3]
What is the difference between toReversed and reverse in JavaScript?
let data = [10, 20, 30];
let rev = data.reverse();
console.log(rev); // [30, 20, 10]
console.log(data); // [30, 20, 10]
let nums = [10, 20, 30];
let safe = nums.toReversed();
console.log(safe); // [30, 20, 10]
console.log(nums); // [10, 20, 30]
How do you use JavaScript toReversed Function with strings?
let word = "hello";
let reversed = word.split("").toReversed().join("");
console.log(reversed); // "olleh"
Does JavaScript toReversed Function work in all browsers?
- Supported in Chrome 110+
- Supported in Firefox 115+
- Supported in Edge 110+
- Not in Internet Explorer
Similar Reads
Unary operators in JavaScript work with only one value. They can change, test, or change the type of that value,…
You may need to loop through properties when you work with objects in JavaScript. The for...in loop helps you do…
Some numbers do not stay whole. You get decimal parts when you divide or calculate prices. You may need to…
JavaScript gives you the Math.trunc() function to work with decimal numbers. You use it when you want to remove the…
Math.exp is a built-in JavaScript function that returns the value of e raised to a given number. Here, e is…
JavaScript works with different kinds of values. Each value has a type, known as a data type. These types help…
The JavaScript unshift function adds one or more elements at the start of an array. It shifts existing items to…
You have to organize your JavaScript code structure. A clean layout helps you read and fix your code. Whether you…
JavaScript added Math.cbrt to solve cube roots. Before that, developers used custom code or Math.pow with 1/3. It works with…
JavaScript gives you the Math.round() function to deal with decimal numbers. You use it when you want to round a…