Use Destructuring Assignment to Assign Variables from Objects I have a question

Tell us what’s happening:
Can someone please tell me why we have to use this

const {tomorrow: tempOfTomorrow} = avgTemperatures;

instead of this one, because I thought that when declaring "AVG_TEMPERATURES’ with “const”, it would also work inside the function.

const {tomorrow: tempOfTomorrow} = AVG_TEMPERATURES;

Your code so far


const AVG_TEMPERATURES = {
  today: 77.5,
  tomorrow: 79
};

function getTempOfTmrw(avgTemperatures) {
  "use strict";
  // change code below this line
  const {tomorrow: tempOfTomorrow} = avgTemperatures; // change this line
  // change code above this line
  return tempOfTomorrow;
}

console.log(getTempOfTmrw(AVG_TEMPERATURES)); // should be 79

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) coc_coc_browser/77.0.126 Chrome/71.0.3578.126 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-objects

AVG_TEMPERATURES is a variable used to test that your code works. If you delete

const AVG_TEMPERATURES = {
  today: 77.5,
  tomorrow: 79
};

Your function should still work if you pass it an object with a tomorrow field, like

getTemperatureOfTmrw({tomorrow: 45})

If you use the test variable, that cannot happen, the function will not work. Plus, if you are using the test variable, you are basically saying the temperature tomorrow will always be 79 degrees, and yea I know spring is coming but it’s still definitely not going to be that hot tomorrow where I am

1 Like

Oh, t got it, thank you.