I need some clarification

I need some clarification


Hey campers! I’m currently working towards my Data Structures and Algorithms certification, and I’m really confused on the concept of ES6s Destructuring Assignment to assign object variables. Could anyone please give me a run down? Thanks, in advance!


Patrick Apgar


Create an object, set properties, assign those to variables for later use (long way):

// create:
const myObj = new Object();
// set properties
myObj.firstName = 'Dan';
myObj.lastName = 'Couper'

// At some point later on...

// Assign properties to variables:
const firstName = myObj.firstName;
const lastName = myObj.lastName;

Create an object, set properties, assign those to variables for later use (short way):

const myObj = { firstName: 'Dan', lastName: 'Couper' };

// At some point later on...

const { firstName, lastName } = myObj;

Notice how it is the reverse in the second example. You are describing a pattern of what the object looks like. It is the same as the first example, but more explicit — instead of passing the whole object, you are saying in one assignment declaration that you want these specific properties from the object


Ah, I see! But I’m somewhat confused now, I wrote this code on the challenge I’m stuck on:

 const { tempOfTomorrow: tomorrow} = AVG_TEMPERATURES;

But I receive this error:

tempOfTomorrow is not defined

What can I change here?

Thanks again! Your first response was a great help!! :slight_smile:


AVG_TEMPERATURES is just a test variable to make sure the function works. This seems to trip everyone up, I wish it was called something different. You just need to use the actual parameter of the function


The reason you are receiving this particular error is because tempOfTomorrow is not a valid property of AVG_TEMPERATURES and thus you are getting undefined.


So I took your helpful hints, and I got it to work!!

Thanks so much! You’ve both been a great help, and now I understand :slight_smile: