DEV Community

Randy Rivera
Randy Rivera

Posted on

Use Destructuring Assignment to Assign Variables from Objects

Destructuring allows you to assign a new variable name when extracting values. You can do this by putting the new name after a colon when assigning the value.

  • Using the same object from the last example:
const HIGH_TEMPERATURES = {
  yesterday: 75,
  today: 77,
  tomorrow: 80
};
Enter fullscreen mode Exit fullscreen mode
  • Here's how you can give new variable names in the assignment:
const { today: highToday, tomorrow: highTomorrow } = HIGH_TEMPERATURES;
Enter fullscreen mode Exit fullscreen mode

You may read it as "get the value of HIGH_TEMPERTURES.today and assign it to a new variable named highToday" and so on. The value of highToday would be the value 77 and the value of highTomorrow would be 80.

Top comments (0)