Destructuring assignment feature provides an easy, readable way to handle object and array assignments.
ES6 introduced assignment destructuring, which provides a clean and more readable way to assign variables.
The old way -
1
2
3
4
constfruits={apple:{color:"red"},orange:{color:"orange"}};constappleTheFruit=fruits["apple"];console.log("color of apple: ",appleTheFruit.color);// red
The new way..
1
2
3
4
5
constfruits={apple:{color:"red"},orange:{color:"orange"}};const{apple}=fruits;// or also include orange: const { apple, orange } = fruits;
console.log("color of apple: ",apple.color);// red
The new syntax gives us a quick and easy way to get named props within an object.