DEV Community

Muhammad Awais
Muhammad Awais

Posted on

3 2

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)

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay