Object Destructuring: The Role of the Spread Operator

Learn the importance of the spread operator’s role in extracting extra or fewer values and copying the whole object.

Extracting with ...

In the previous lessons on object destructuring examples, we have extracted part of the object, leaving behind some properties. Sometimes, we want to copy the whole object, and at the same time, maybe add new properties or change the values of existing properties. Destructuring can do this quite well.

Using destructuring to copy object properties

For example, the popular JavaScript state container library Redux is based on the immutable state. Instead of modifying existing objects, it transforms them into new objects. Typically, a transformation may change one or two property values or add a new property while copying the bulk of the existing object’s properties.

Let’s take a look at a poor approach to copying first and see why it should be avoided, especially when using libraries like Redux.

Get hands-on with 1400+ tech skills courses.