Use Destructuring Assignment to Assign Variables from Objects (IRL Question)

Tell us what’s happening:
Can somebody help me out with understanding when this would matter in real world application? When I use the first option, it does not pass the tests. If I use the second option, it works and passes the tests. I understand what is happening, I guess I am more trying to understand the necessity and utility of this as it seems to make more sense to just use the Object name.

const {tomorrow : tempOfTomorrow} = AVG_TEMPERATURES; //incorrect but still works
const {tomorrow : tempOfTomorrow} = avgTemperatures; //correct

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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 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/

the tests in this challenge are checking what you have written in the editor and expect you to use the function parameter, which is the best practive when you have a function parameter and you need to work on something passed in as function argument

Imagine a scenario where you have an object with loads of properties, and introduce closure, so none of the variable names are available globally and you’ll find some utility in passing a parameter. The variable that was available in the learning example no longer exists.

You could correctly argue in this and many other cases that the function is unnecessary, just get the property, as the variable valueFromInstance shows.

class Thing {
  constructor(a,b) {
    this.today = a;
    this.tomorrow = b;
    this.lat = 0;
    this.lon = 0;
    this.conditions = ["some condition"]
  }
}

start()

function start() {
  const thing = new Thing(20,30);
	const valueFromFn = getTempOfTmrw(thing);
	const valueFromInstance= thing.tomorrow 
  console.log(valueFromFn , valueFromInstance)
}



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

Where I see some utility is in destructuring arrays to make sense of them, such as the cars example below

class Car {
  constructor(values) {
    const [speed, doors, colour, fuel, capacity] = values
    this.speed = speed;
    this.doors = doors; 
    this.colour = colour;
    this.fuel = fuel;
    this.capacity = capacity
  }
}

start();

function start() {
  const cars = [
    [10,5,'red','diesel','2l'],
    [12,5,'black','petrol','2.5l'],
  ]
  const carInstances = cars.map(car=>new Car(car))
  console.log(carInstances)
  const [car1, car2] = carInstances;
  console.log(car1.speed, car2.speed)
}

Hopefully I understood the question :slight_smile: