Question about ES6 Use Destructuring Assignment to Assign Variables from Nested Objects

const LOCAL_FORECAST = {
  today: { min: 72, max: 83 },
  tomorrow: { min: 73.3, max: 84.6 }
};

function getMaxOfTmrw(forecast) {
  "use strict";
  // change code below this line
  const{tomorrow : {max: maxOfTomorrow}} = forecast; // change this line
  // change code above this line
  return maxOfTomorrow;
}

console.log(getMaxOfTmrw(LOCAL_FORECAST)); // should be 84.6

I just want to make sure I understand everything. In the end why do we console.log (LOCAL_FORECAST) instead of (forecast)?

First of all, forcast is a variable that only has scope in the function getMaxOfTmrw so that would throw an error. Plus, inside that function, it is an object. You could get the same output with:

console.log(LOCAL_FORECAST.tomorrow.max);

but that wasn’t the point of the exercise. This also could have been done without a function:

const LOCAL_FORECAST = {
  today: { min: 72, max: 83 },
  tomorrow: { min: 73.3, max: 84.6 }
};

const { tomorrow : { max: maxOfTomorrow } } = LOCAL_FORECAST; 

console.log(maxOfTomorrow);

That also uses destructuring. I’m not sure why they added the complexity of a function, but they did. Perhaps it was to keep people from “cheating” like I did in the first one.

But they are silly little problems. It’s more important that you understand destructuring as it is used here.