DEV Community

Randy Rivera
Randy Rivera

Posted on

1

Using Destructuring Assignment to Extract Values from Objects

Destructuring assignment is special syntax introduced in ES6, for neatly assigning values taken directly from an object.

  • Consider the following ES5 code:
const HIGH_TEMPERATURES = {
  yesterday: 75,
  today: 77,
  tomorrow: 80
};

const yesterday = HIGH_TEMPERATURES.yesterday;
const today = HIGH_TEMPERATURES.today;
Enter fullscreen mode Exit fullscreen mode
  • yesterday would have a value of 75, and would have the value 77.

  • Now let's Replace the two assignments with an equivalent destructuring assignment. It should still assign the variables today and tomorrow the values of today and tomorrow from the HIGH_TEMPERATURES object.

const { today, tomorrow } = HIGH_TEMPERATURES;
Enter fullscreen mode Exit fullscreen mode
console.log(today); will display 77
console.log(tomorrow); will display 80
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Neon image

Next.js applications: Set up a Neon project in seconds

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay