ES6 Destructuring Assignment

Michele Riva
openmind
Published in
5 min readMay 19, 2019

--

Destructuring Assignment is an amazing feature introduced with EcmaScript 2016, which is now available in both browsers and Node.js.

If you’re writing CommonJs or ES6 modules, you’re probably already using it! Let’s pretend we have a file called math.js, where we have a bunch of functions to be exported:

math.js

If we create a new file, let’s say index.js, we can import the functions above in block:

index.js

But now imagine if our math.js file had hundreds of functions. Why do we import them all, if we only need (for example) math.double?
Here comes the concept of object destructuring:

index.js

Our maths.js file exports an object containing every exported function. So, if we don’t want to import a lot of useless functions from that file, we can just destructure the exported object and get back only the functions we…

--

--