Last updated Apr 1, 2021

Object Destructuring JavaScript

In this tutorial, we are going to learn about object destructuring in 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',
  age:'22'
};


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

Note : When we destructure an array we use square brackets, when we destructure an object we use curly braces.

Using different property name

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

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

Setting Default 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 users = {
  name: 'Joy',
  address: {
    city: 'san francisco'
  }
};

// Object destructuring:
const { address: { city } } = users;

city; // => 'san francisco'

Rest object after destructuring

const users = {
  fname: 'Joy',
  fullName: 'Joy Benrji',
  job:'developer'
};

const { name, ...fullData } = users;

fullData ; // => { fullName: 'Joy Benrji' }

In above example, the destructuring const { name, ...fullData } = users extracts the property fname and the remaining properties (fullName,job) are collected into the variable fullData.

Tags

Available for projectsLet’s build together

Need this built properly,
not just explained?

I take on Angular and NestJS work directly — auth systems, multi-tenant SaaS, API rewrites and the deploy pipeline underneath them. You talk to the person writing the code.

Projects delivered
20+ Projects delivered
Angular • NestJS
7+ years Angular • NestJS
Average reply time
1 day Average reply time

What I can help with

  • Auth and permissions systems
  • Multi-tenant SaaS foundations
  • API design and rewrites
  • Deploy pipelines and VPS setup
  • Bug fixing and performance optimization
Send me a message

Tell me about your project and I’ll get back with a realistic scope and price.

Turn
Your Ideas
Into Reality

Stay in the loop

Get the finished tutorials, projects and resources in your inbox.

Join 5,000+ developers and get new tutorials, source code, product updates and exclusive content — straight to your inbox.

  • No spam
  • Useful content only
  • Unsubscribe anytime

Subscribe to TutsCoder

Be the first to get new tutorials, projects and tips.

Join 5,000+ developers already learning with us.

Learn
Build
Grow

Discussion

Comments

Share your thoughts, ask questions or suggest improvements. Let’s learn and grow together!

Loading comments…

Join the discussion

Sign in to join the discussion. Your account also keeps your saved tutorials and path progress.