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;
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;
console.log(today); will display 77
console.log(tomorrow); will display 80
Top comments (0)