Categories
JavaScript Answers

How to sort an array without mutating the original array with JavaScript?

Spread the love

To sort an array without mutating the original array with JavaScript, we make a copy of the original array.

For instance, we write

const sorted = [...arr].sort();

to make a copy of arr with the spread operator.

And then we call sort to sort the copied array.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.