ES6: Use Destructuring Assignment to Assign Variables from Objects Question

Hi Fellow FreeCodeCampers!

I’m new to JavaScript and working through the list of assignments on free code camp. I’m on the lesson -
ES6: Use Destructuring Assignment to Assign Variables from Objects.

Now there are 3 examples they give, all which makes sense to me:
Example #1:
var voxel = {x: 3.6, y: 7.4, z: 6.54 };
var x = voxel.x; // x = 3.6
var y = voxel.y; // y = 7.4
var z = voxel.z; // z = 6.54

Example #2:
const { x, y, z } = voxel; // x = 3.6, y = 7.4, z = 6.54

Example #3:
const { x : a, y : b, z : c } = voxel // a = 3.6, b = 7.4, c = 6.54

Now the problem given is:
Use destructuring to obtain the length of the input string str, and assign the length to len in line.

This is the original code to start with:

function getLength(str) {
“use strict”;

// change code below this line
const length = 0; // change this
// change code above this line

return len; // you must assign length to len in line

}

console.log(getLength(‘FreeCodeCamp’));

Ok - so now I’m confused. If I simply use .length, I get the error that I haven’t used destructuring to obtain the length. I have two questions,
#1 why would we use destructing to do this when there is a simple answer like .length
#2 how do we use destructing for this?

// make this change
const len = str.length;

In the example the only thing I see are ways to assign properties of an array to a const variable, but I’m having difficulty even starting. I don’t understand how this would be used to get the length of a variable.

Anyone have any suggestions on how to break this problem down or explain what I am missing?

2 Likes

Hi
length is a property of str
the same way as
x is a property of voxel

In the third example
assign local variable a the value of voxel.x
and as in the challenge
assign local variable len the value of str.length

They aren’t saying this is the smart way to do this. They are testing if you can to see if you understand destructuring assignment. So yeah, it seems like a strange way to do this.

len needs to be defined

len needs to = str

You’ll need to take length and copy it into len (as demonstrated in example three)

Hi and thanks alhazen and mjbenefiel. Both of your suggestions were helpful - I’m starting to get into the parts of Javascript that are more difficult for me to read. A lot of the earlier lessons, when I read them they made sense logically.

But for some reason, these seem to be less logical when I read through the code. I understand the concept of length needs to be copied to len via { length : len }.

But I originally thought that would be accomplished by this:
const { length : len };

I’m a little confused about why I need to add the = str. Since we already got the length via the length variable, I’m not sure what, if anything is being assigned from str at this point, or why that part of the line needs to be added.

Anyone able to explain in a common sense way?

I tried this and it worked:

function getLength(str) {
“use strict”;

// change code below this line
const length = str.length; // change this

const { length : len } = str;

// change code above this line

return len; // you must assign length to len in line

}

console.log(getLength(‘FreeCodeCamp’));

2 Likes

Ok, so as mjbenefiel pointed out, the length is a property like x in the examples. Since the property already exists, we don’t have to assign the property through declaration.

Ok, so taking str and copying the length onto len via:
const { length : len } = str;

Thanks randelldawson.

1 Like

thanks! very helpful.

thnx for the help both :smiley: now i get the challenge

1 Like

I think this is the best example of an asked question I’ve seen so far, bravo!

1 Like

const {length: len} = str;

did the job

I don’t really get why

this part doesn’t need to look like

const { length : len } = str.length;

I understand that destructuring is assigning values taken directly from an object but why it knows which value it has to take? In this case - length?

Thanks! That really helps as I’m not always sure which words/functions are standard and which are made up by me… I guess I need to be more careful while doing and reading exercises.

thank u sir its very helpful.