DEV Community

Discussion on: JavaScript object destructuring usages you must know

Collapse
 
prajapatiparth profile image
Parth Prajapati

Here's one more for you.

// using destructuring to optionally add properties to object;

const isEmployee = true;

const person = {
  name: 'xyz',
  age: 28,
  ...isEmployee ? {     // if isEmployee is true, it will destructure the employee object
    employeeId: 'G548',
    department: 'procurement'
  } : {},   // else it will destructure this empty object
};
Enter fullscreen mode Exit fullscreen mode
Collapse
 
atapas profile image
Tapas Adhikary

Super. Thanks!