DEV Community

Cover image for JavaScript - toReversed() Method
Kristiyan Velkov
Kristiyan Velkov

Posted on • Edited on

1 1 1 1 1

JavaScript - toReversed() Method

The toReversed() method is used to create a new array that is the reverse of the original array. The original array remains unchanged.

Here's an example to illustrate the difference between toReversed and reverse:

const numbers = [1, 2, 3, 4, 5]; 
const reversedNumbers = numbers.toReversed(); 

console.log(numbers); // Output: [1, 2, 3, 4, 5] 
console.log(reversedNumbers); // Output: [5, 4, 3, 2, 1]
Enter fullscreen mode Exit fullscreen mode

The toReversed method returns a new array with the elements in reverse order, while the original array remains unchanged.

On the other hand, the reverse method modifies the original array in place. Here's an example:

const numbers = [1, 2, 3, 4, 5]; 
numbers.reverse(); 

console.log(numbers); // Output: [5, 4, 3, 2, 1]
Enter fullscreen mode Exit fullscreen mode

toReversed() creates a new array while reverse() modifies the original array.


Browser's compatibility

Image description


Image description

linkedin


Image description

If you like my work and want to support me to work hard, please donate via:

Revolut website payment or use the QR code above.

Thanks a bunch for supporting me! It means a LOT 😍

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Cloudinary image

Zoom pan, gen fill, restore, overlay, upscale, crop, resize...

Chain advanced transformations through a set of image and video APIs while optimizing assets by 90%.

Explore

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay