Destructuring in variable assignment

Posted on

If the name of your variable is the exact same as the property you would extract from an object, you can use destructuring instead. That way, you don’t have to type the same word twice.

We can also rename the variable we’re destructuring into at the same time.

const person = {
  name: "Bob",
  age: 35,
  email_address: "bob@example.com",
}
 
// without destructuring
const age = person.age
const emailAddress = person.email_address
 
// with destructuring
const {
  age,
  email_address: emailAddress,
} = person
const person = {
  name: "Bob",
  age: 35,
  email_address: "bob@example.com",
}
 
// without destructuring
const age = person.age
const emailAddress = person.email_address
 
// with destructuring
const {
  age,
  email_address: emailAddress,
} = person
Debug
none
Grid overlay