Learning about spread, destructuring, and rest
In ES6+, there are new methods for handling object copying and displaying variables and parameters. These capabilities go a long way in making JavaScript code both shorter and easier to read. These features have become standard practice in modern JavaScript, so it is important that we know about them and use them properly.
Spread, Object.assign, and Array.concat
The spread, Object.assign
, and Array.concat
JavaScript features are fairly similar. Basically, you are appending multiple objects or arrays together into one object or array. However, strictly speaking, there are some differences.
In the case of objects, there are two ways of merging or concatenating objects:
- Spread—for example,
{ … obja, …objb }
: You are creating a non-modified copy of these two objects and then creating a brand-new object. Note that spread can handle more than just two objects. Object.assign
—(obja, objb)
: You are...