object destructuring javascript

object destructuring javascript

In this tutorial, we are going to learn about object destructuring in javascript.

So, let's get started...

What is Destructuring?

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

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

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

const sampleObj = {
  name: 'Adam',
  address: 'New york, 21',

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

Using different property name

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

//Output : Adam, 22

Setting Default values

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

//Output : [],[]

Mutating variables

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

Nested Objects

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


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