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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
const personalInfo = { name: "Parwiz", lname:"Forogh", email:'par@gmail.com', phone:345666 } const firstname = personalInfo.name; const lname = personalInfo.lname console.log(firstname); console.log(lname) |
Now using destructuring, it is so easy and simple.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
const personalInfo = { name: "Parwiz", lname:"Forogh", email:'par@gmail.com', phone:345666 } const {name, email} = personalInfo console.log(name); console.log(email); |
Also you can do array destructuring.
1 2 3 4 5 6 7 8 9 10 |
const displayInfo = function() { return ['Parwiz', 'Forogh', 'par@gmail.com'] } let [name, lname] = displayInfo(); console.log(`Name is ${name} and lname is ${lname}`); |
Exercise Files
No Attachment Found