UseDestructuring Assignment to Assign Variables from Objects


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

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

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


  1. You’re not using the function parameter, you’re using the test object used to check your code works
  2. There isn’t a property with the key “tempOfTommorow” on the object, so the destructuring will fail
1 Like

where will i add the let tempOfTommorrow = tommorrow in the code block

Look at the example: you are giving the value of the property tomorrow a name (tempOfTomorrow). So it should look like an object definition, but the value of the property is the name.

This is the shorthand for creating an object and assigning it to a variable (exampleObj):

const exampleObj = {
  exampleProperty: 1,
  anotherProperty: 2,
};

This is the shorthand for accessing an object and assigning the value of one of its properties to a variable (see how it is the inverse of the above):

const { exampleProperty } = exampleObj;

(exampleProperty is now the name of a variable, and the value of it is 1)

This is the shorthand for accessing an object and assigning the value of one of its property to a variable with a different name to the actual name of it on the original object:

const { exampleProperty: exProp } = exampleObj;

(exProp is now the name of a variable, and the value of it is 1)

1 Like

This is how i did it
Thanks for your help

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
1 Like