Modern JavaScript
About Lesson

In this JavaScript lesson we are going to learn about JavaScript Destructuring Object, so destructuring helps in unpacking values from an array or an object. you can use with object and arrays, we can use it with arrays and objects.



OK now let’s create an example, in here we have an object. if you want to access to any item of the object than you need to use object name with dot operator, as we have done in here.



Now using destructuring, it is so easy and simple.




Also you can do array destructuring.



JavaScript Destructuring Object Features

Destructuring is a feature in JavaScript that allows you to extract values from objects and arrays and assign them to variables. these are some features of object destructuring in JavaScript:


  1. Basic Object Destructuring: You can use object destructuring to extract values from an object and assign them to variables with the same name as the object’s properties.

In this example person object is destructured to extract the name, age and gender properties and assign them to variables with the same names.



  1. Default Values: You can provide default values for variables in case the value in the object is undefined.

In this example, the gender property is not present in the person object, so the default value of “unknown” is used.



  1. Renaming Variables: You can rename variables using object destructuring by providing a new name after a colon.

In this example, the name property is renamed to fullName and the gender property is renamed to sex.



In result we can say that object destructuring in JavaScript is powerful feature that can simplify code and make it more readable. It allows you to extract values from objects and assign them to variables using a concise and flexible syntax.