JavaScript toReversed Function with Examples

javascript toreversed function

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.

AspecttoReversedreverse
Mutates OriginalNoYes
Return ValueNew reversed arraySame array reversed
Use CaseKeep original intactChange 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:

  • toReversed makes a reversed copy without change to the source.
  • reverse changes the array itself and returns it.

FAQs

What is JavaScript toReversed Function?

JavaScript toReversed Function creates a new array with reversed elements. It does not change the original array. Example code:

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?

reverse() changes the original array. toReversed() returns a new array without touching the old one.

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?

You must split the string into characters, then call toReversed(), and finally join them again.

let word = "hello";
let reversed = word.split("").toReversed().join("");

console.log(reversed); // "olleh"

Does JavaScript toReversed Function work in all browsers?

toReversed() works in modern browsers only. It is part of ECMAScript 2023. Use polyfills for old browsers.
  • Supported in Chrome 110+
  • Supported in Firefox 115+
  • Supported in Edge 110+
  • Not in Internet Explorer

Similar Reads

Top 5 Code Editors: The Best IDE for JavaScript

If you are a coder, one of your primary requirements is to have a trustworthy code- editor. A Code editor…

JavaScript Math.max(): How to Find the Highest Number

Math.max() is a built-in JavaScript function that returns the highest number from a list of values. It has been part…

JavaScript Math abs(): How It Works with Examples

Some numbers come from user input and other come from math operations. Sometimes, these numbers need to stay positive. The…

JavaScript Introduction: What is JavaScript?

The current essay is devoted to the basic principles and introduction of JavaScript. This language no longer needs to be…

JavaScript While Loop: How It Works with Examples

The JavaScript while loop runs code as long as a condition stays true. You can use it to repeat tasks…

JavaScript Comparison Operators: How == and === Work

You need to compare values in almost every JavaScript program. This is where comparison operators come in. They check if…

JavaScript Hoisting: How It Works with Examples

You will face hoisting early when you write JavaScript. It affects how your code runs, and it may confuse you…

JavaScript Logical Operators with Examples

Logical operators in JavaScript let you check conditions and combine multiple checks in your code. In this guide, you will…

JavaScript Polyfilling & Transpiling Guide for Beginners

JavaScript moves fast, and new features come. Some browsers do not support these features and cause issues for users. Developers…

Print in JavaScript Console Using Log, Info, Warn, and Error

The JavaScript console is a tool in web browsers that helps you print the result to the web browser. It…

Previous Article

HTML vs XHTML: Key Differences Every Developer Must Know

Next Article

PHP array_flip Function: How it Works with Examples

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.