TIL sort mutates the array
| 1 min read
While working on a bug at work, I found out that sort
mutates the array & was actually the cause of the bug. MDN actually mentions this here too.
So the trick to avoid any potential bugs is to make a copy of the array & then call sort
on the copy of that array & use that for further manipulations.
Lucky for us in Javascript (or ES6), we can make a copy of an array like this:
const copyOfArray = [...sampleArray]