Tip 8 Avoid Push Mutations with the Spread Operator

In this tip, you’ll learn how to avoid array mutations by creating new arrays with the spread operator.

As you’ve just seen, mutations can have unexpected consequences. If you change something in a collection early in the code, you can create a bug much deeper. Mutations may not always cause major headaches, but they do have that potential, so it’s best to avoid them when possible. In fact, some popular JavaScript libraries (such as Redux) won’t allow functions with any mutations at all.

Plus, a lot of modern JavaScript is functional in style, meaning you’ll need to write code that doesn’t contain side effects and mutations. There’s a lot to be said about functional JavaScript, ...

Get Simplifying JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.