DEV Community

Thabitha Kavyu
Thabitha Kavyu

Posted on

Destructuring an object

const Baby = {
  name: 'Tabby',
  favColor: 'brown',
  Age: 21
};
const {name, ...rest } = Baby;
console.log(name); // 'Tabby'
Enter fullscreen mode Exit fullscreen mode

Discussion (1)

Collapse
tabithakavyu profile image
Thabitha Kavyu Author

Using the rest operator ..., you can store all remaining properties from the original object to a variable.

   const { name, ...rest}= Baby;
   console.log(rest); // 
  {favColor: "Tabby", Age: 21}