FAQ: Advanced Objects - Destructured Assignment

This community-built FAQ covers the “Destructured Assignment” exercise from the lesson “Advanced Objects”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Destructured Assignment

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Why in this exercise do we have to call the method with () following? Just a few lessons back we were taught that using () following a method call is unnecessary.

I believe the only time we were told that a method didn’t need to be called with parenthesis was during the “Getters” and “Setters” lessons. Therefore, that wouldn’t apply to “Destructured Assignments”.

4 Likes

In the code below, the console prints “stay inside” is that because we referenced the variable or the key? Do they need to be called the same thing? When I play around with changing the name of either it appears to only work if the key, variable and console log are all called the same thing…

const { functionality } = robot;
console.log(functionality.beep());
const vampire = {
name: ‘Dracula’,
residence: ‘Transylvania’,
preferences: {
day: ‘stay inside’,
night: ‘satisfy appetite’
}
};
const { day } = vampire.preferences;
console.log(day);

1 Like

Hi;
in lesson explanation, to call inside a method it used:
const { day } = vampire.preferences;
console.log(day); // Prints ‘stay inside’

While the script was:
const vampire = {
name: ‘Dracula’,
residence: ‘Transylvania’,
preferences: {
day: ‘stay inside’,
night: ‘satisfy appetite’
}
};

But in exercise, why it gave me error when I write:
const {beep} = robot.functionality;

to relate:
const robot = {
model: ‘1E78V2’,
energyLevel: 100,
functionality: {
beep() {
console.log(‘Beep Boop’);
},
grateful
martinmiciciday

I notice in the examples for destructured assignment that the object’s key is not only wrapped in curly braces, but surrounded by spaces as well. When testing, I discovered that this is not essential.

Is it convention to surround the key with spaces when using destructured assignment?

2 Likes

Im with emg… why do we need to call it in the “Destructured Assignment” exercise but not in the “Factory Function” tinCan exercise?

I believe getters & setters are methods but slightly special methods, essentially properties (I think) so the rule of invoking with () doesn’t apply to them

Because on the previous lesson we learn getter and setter that they don’t use parenthesis to call a function. Here we don’t use them, so calling functions need to use parenthesis.

Because beep is a function. You should surround your properties in curly braces instead of a function.

In order to call the .beep() method on functionality , we need to write two lines.

const {functionality} = robot;
functionality.beep();
// output: Beep Boop

However, the {functionality} in the curly brackets just shorthand for robot.functionality- and thus, robot.functionality.beep(); will log Beep Boop as well.

So my question is, why would writing const {functionality.beep()} = robot result in an error? Shouldn’t it log Beep Boop to the console as well?