DEV Community

Discussion on: 13 useful JavaScript array tips and tricks you should know

Collapse
 
mc100s profile image
Maxence Bouret

Thanks for the article :)

Tip 11 (Reversing an array) is also modifying the original array colors.

Instead, if I don't want to change the original array, I would write:

var colors = ["blue", "white", "green", "navy", "pink", "purple", "orange", "yellow", "black", "brown"];
var reversedColors = [...colors].reverse();
console.log(reversedColors); // returns ["blue", "white", "green", "navy", "pink", "purple", "orange", "yellow", "black", "brown"]
Enter fullscreen mode Exit fullscreen mode
Collapse
 
catalin560 profile image
catalin560

or colors.slice().reverse() works too