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
};
- Here's how you can give new variable names in the assignment:
const { today: highToday, tomorrow: highTomorrow } = HIGH_TEMPERATURES;
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)