Skip to content
loading...

Destructuring Objects in javascript

github logo ・1 min read  

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'
}

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

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

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

twitter logo DISCUSS
Classic DEV Post from Jul 30 '19

PublishTo.Dev: Scheduling article publishing on dev.to

Muhammad Awais profile image
ReactJS | Angular | JavaScript | Frontend | Ruby on Rails | User Interface | Ionic | Heroku | AWS

Sore eyes?

dev.to now has dark mode.

Go to the "misc" section of your settings and select night theme ❤️