Photo by Ian Froome on Unsplash

#8 Functional Approach: Destructuring

Ricardo Luz
2 min readApr 19, 2018

--

When we call a JS function we have problems to identify the parameters name, once we only send the argument, like in the example below:

The problem is, the arguments have no name, but they have a specific order, depending on your code this is a door to create bugs once is easy to put this parameter in a different order.

This case is only one of the things that destructuring helps you to solve! Using destructuring you could nominate your parameters using a simple array, like this:

Cool right? Now the order doesn’t matter anymore, and you only needed to add “{“ before your parameters and “}” after.

But there is more, using this technique you could define a default value for your parameters like this:

Also, you can use destructuring to extract from a long array only the keys that you want to use and transform this keys into a new variable, like this:

If you to want to know more about Javascript I recommend these books:

  • You Don’t Know JS Serie by Kyle Simpson (My favourite)
  • Eloquent JS by Marijn Haverbeke
  • Head First Javascript by Michael Morrison
  • Secrets of the JavaScript Ninja by Bear Bibeault and John Resig

Thanks for reading! Feel free to hit the recommend button below if you found this piece helpful.

You can connect with me on Twitter: @_rxluz or in my Website.

If this post was helpful, please click the clap 👏 button below to show your support! ⬇⬇

--

--