DEV Community

Barrios Freddy
Barrios Freddy

Posted on • Edited on

4 1

Object destructuring assignment

The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects. In this case, we are going to see how destructuring works for objects.

const user = {
  userName: 'fbarrios',
  firstName: 'Freddy',
  lastName: 'Barrios',
  address: {
    street: '52',
    zipcode: '110007',
}

const { firstName } = user
console.log(firstName); // Freddy

Enter fullscreen mode Exit fullscreen mode

This is the same as…

const firstName = user.firstName
Enter fullscreen mode Exit fullscreen mode

It is possible to set default values, in case the property doesn’t exist or is undefined.

const { phone = '0000000' } = user
console.log(phone) // 0000000
Enter fullscreen mode Exit fullscreen mode

A property can be renamed when it’s destructured.

const { username: displayName } = user
console.log(displayName); // Freddy
Enter fullscreen mode Exit fullscreen mode

You can rename and set a default value for a property

const { phone: phoneNumber = '0000000' } = user
console.log(phoneNumber) // 0000000
Enter fullscreen mode Exit fullscreen mode

When the object has another one nested it’s destructured in the following way

let { address: { street } } = user
console.log(street) // 52
Enter fullscreen mode Exit fullscreen mode

Destructuring can be used with the arguments of a function

function fullname({firstName, lastName}) {
  return `${firstName} ${lastName}`;
}

console.log(fullName(user)); // Freddy Barrios
Enter fullscreen mode Exit fullscreen mode

Computed property names can be used with the destructuring assignment.

const key = 'username'
const { [key] } = user
Enter fullscreen mode Exit fullscreen mode

Rest operator in object destructuring

This operator (...) collects all properties that weren’t picked off during object destructuring

const { address, …restProperties } = user
console.log(address);
/*
{
  street: '52',
  zipcode: '110007',
}
*/ 
console.log(restProperties); /
{
  userName: 'fbarrios',
  firstName: 'Freddy',
  lastName: 'Barrios',
}
*/
Enter fullscreen mode Exit fullscreen mode

As you can see, object destructuring is very useful and gives us another way to write better code.

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

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

Learn more

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