Destructuring
Destructuring is a way to quickly extract data out of an {}
or []
without having to write much code.
To borrow from the MDN, destructuring can be used to turn the following:
into
This is pretty interesting, but at first it might be hard to see the use case. ES6 also supports object destructuring, which might make uses more obvious:
Destructuring can also be used for passing objects into a function, allowing you to pull specific properties out of an object in a concise manner. It is also possible to assign default values to destructured arguments, which can be a useful pattern if passing in a configuration object.
There are many more sophisticated things that can be done with destructuring, and the MDN has some great examples, including nested object destructuring and dynamic destructuring with for ... in
operators".
Last updated