TIL: Destructuring function arguments in JavaScript

July 22, 2016JavaScriptES6

TIL: Destructuring function arguments in JavaScript

So check this out! If you have a function that accepts an object as an argument and you don't want pull each of the properties of that argument out one by one, what a hassle, right?, you can destructure the argument in the function argument signature to access each of the arguments properties directly.

Here it is in action.

// the arguments below are the destructured parts
// notice how there's what looks like an object in the parens
function printDestructuredArgument({ one, two }) {
  console.log(one); // option one
  console.log(two); // option two
}

const args = {
  one: "option one",
  two: "option two",
};

printDestructuredArgument(args);

How cool is that?!

The author, Jason Merino, with sun glasses and a hat

Jason Merino 💻 🚀

Software engineer, TypeScript enthusiast, avid gardener, all around family man, Franciscan at heart, celiac, aphantasiac. I enjoy nature and a good technical manual.

Follow me on Twitter and checkout my code on Github!