Mutation === 💩

One of the many things I’ve picked up from the excellent Syntax podcast over the years is mutation should be avoided. Today a came across a great example of this with the reverse array method.

I had some code that looked something like this:

To my surprise the resulting output of the console logging was identical. Both output the array in the same order. So what’s going on here?

As it turns out, the reverse method actually mutates the original array. Obviously if you don’t know this (like I didn’t), this can be problematic. The best way around this shorting coming is to spread the array into a new one, then reverse it like this: