GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||
GeezerVison is showing flatcoding.com — JavaScript toReversed Function with Examples - FlatCoding Home JavaScript toReversed Function with Examples
Table of ContentJavaScript 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
|
| 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.
toReversed Function in JavaScriptReverse 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.
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.
let numbers = [1, 2, 3];
let newArray = numbers.toReversed();
console.log(newArray); // [3, 2, 1]
console.log(numbers); // [1, 2, 3]
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]
let word = "hello";
let reversed = word.split("").toReversed().join("");
console.log(reversed); // "olleh"
The JavaScript nullish coalescing operator (??) gives a default value when a variable is null or undefined. Understand the Nullish…
JavaScript arithmetic operators let you add, subtract, multiply, or divide numbers. You can also use them to find remainders or…
AJAX keeps your web app fast. You do not need to reload the page every time you send or get…
The Math.acos function finds the angle in radians from a cosine value in JavaScript. It gives a value between 0…
Math.random() gives a number between 0 and 1 in JavaScript. It helps create random values for colors or other data.…
Click a button on a website. Something changes. A menu opens or a message pops up. That’s how JavaScript works.…
JavaScript code follows some rules that help both new and old developers. A project without rules in code creates trouble…
The assignment operator in JavaScript stores and updates values. It helps you hold data in a variable and change it…
JavaScript includes Math.sign to find out if a number is positive. It also helps detect negative values or zero. It…
JavaScript switch statement checks many values without long chains. It appeared to replace stacked conditions that slow you down. Understand…