DEV Community

Muhammad Awais
Muhammad Awais

Posted on

Destructuring Objects in javascript

Destructuring is a better way to get the values from array and objects with a concise code. let’s deep dive into it.

Let’s suppose you have the following object:

const FullName = {
  firstName: 'Muhammad',
  lastName: 'Awais'
}
Enter fullscreen mode Exit fullscreen mode

in the normal scenario, to get the firstname and lastname from the object, you have to create new variables and assign the object values in them, like below:

let firstName = FullName.firstName // Muhammad
let lastName = FullName.lastName // Awais
Enter fullscreen mode Exit fullscreen mode

While in using destructuring you can do above thing in a most better way:

let { firstName, lastName } = FullName

console.log(firstName) // Muhammad
console.log(lastName) // Awais
Enter fullscreen mode Exit fullscreen mode

behind the scenes, {} are telling javascript to generate the aforementioned variables and assign the object values in it, es6 power is used here.

Top comments (0)