javascript

object destructuring javascript

object destructuring javascript

What is Destructuring?

Desctrcring is an ES6 feature and it's basically a way of unpacking values from an array or object into separate variables.

In other words, Destrctring is a way to break a complex data structure down into a smaller data structure like a variable 

 

In Object Destrcring Order of element does not matter like array, so provided exact name of property.

const sampleObj = {
  name: 'Adam',
  address: 'New york, 21',
  age:'22'
};


const {age, name} = sampleObj ;
console.log(age, name);
//Output : 22, Adam

 

//Using diffrent property name

 

const {name:userName,age:ageNow} = sampleObj;

console.log(userName,ageNow);
//Output : Adam, 22

//Setting Deafault values

const {languges=[],certificates = []} = sampleObj;

console.log(languges,certificates);
//Output : [],[]

// Mutating variables

let m = 111;
let w = 999;
const obj = {m:23,w:7,c:14};
({m,w} = obj);
console.log(m,w);
//output : 23 7

 

// Nested Objects

 

const {fri:{open,close}} = openingHours;

console.log(open,close);

 

 

 

 

If You Appreciate What We Do Here On TutsCoder, You Should Consider:

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Leave a Comment