Destructuring Assignment question

Tell us what’s happening:

Ok so… I understand everything (I think) except I don’t understand why:

const {tomorrow : tempOfTomorrow } = avgTemperatures;

is not

const {tomorrow : tempOfTomorrow } = AVG_TEMPERATURES; ?

Where is avgTemperatures even defined in the first place? If the object tomorrow holds the value of 79, under the constant AVG_TEMPERATURES, and you’re trying to assign that to tempOfTomorrow… where does avgTemperatures come from?

Someone else said: "The function takes a parameter.

Instead of using that parameter, you’ve hardcoded in a reference to the object used to test your code works."
…but I don’t understand this.

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; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0.

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

the const AVG_TEMPERATURES object is being passed through the console.log function into the getTempOfTmrw function which is using it as avgTemperatures

1 Like

So anytime a constant passes through a function… it’s converted into a… standard variable format? Or was it declared that way in the line function getTempOfTmrw(avgTemperatures)?

avgTemperatures is the parameter of the getTempOfTomrw function.

That parameter is defined when you pass in the AVG_TEMPERATURES variable, which is an object, as an argument to the function.

So inside the function it’s as if avgTemperatures = AVG_TEMPERATURES.

Does this answer your question? I’m reading this as you are trying to understand the relationship between variables, parameters, and arguments?

1 Like

yes it does now. That’s 3 or 4 subtle little nuances I was not quite understanding. Thank you so much.

1 Like

no its not converted, it was destructed and then assigned tomorrow value to a new variable called tempOfTomorrow and returned it