- ES6 provides the feature of defining and initializing multiple variable in single shot.This process is called Destructuring assignment of variables.
- In this demo, “We will learn to define and initial multiple variables using destructuring”.
- In the following code we have 3 variables name,subject and score are defined and assigned with values from aStudentData object which has about,topic and result.We can think like aStudentData object has unpacked its properties to variables name,subject and score.
var aStudentData = { "about":"Sandeep", "topic":"Computer", "result":256 }; var {about:name,topic:subject,result:score} = aStudentData; console.log("name: "+name); console.log("subject: "+subject); console.log("score: "+score);
- The output of the previous code can be found in the following JSBIN link.