DEV Community

jaepass
jaepass

Posted on • Edited on

4 2

Learning Destructuring

What exactly is the new feature destructuring in ES6?

Destructuring helps to break up your code by giving it the pattern you are expecting to get back. It aims to make your code more readable. Destructuring is an assignment task, not a declarative task.

Lets look at the different between the regular imperative way and with ES6 destructuring technique:

function someData() {
  return [dog, cat, fish];
}

var tmp = someData();
var first = returnedData[0];
var second = returnedData[1];
var third = returnedData[2];

We now take the same logic and implement with ES6 destructuring:

function someData() {
  return [dog, cat, fish];
}

var [
  first,
  second,
  third] 
= someData();

If you want to define a default value, you can use a default value expression for the destructured method:

var [
  first,
  second = 10,
  third
] 
= someData();

Whereas, prior to ES6, you would implement your default value as such:

var second = returnedData[1] !== undefined ? returnedData[1] : 10;

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more