# Use Destructuring Assignment to Assign Variables from Objects

**URL:** <https://forum.freecodecamp.org/t/use-destructuring-assignment-to-assign-variables-from-objects/195283>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [June 1, 2018, 7:39pm UTC](https://forum.freecodecamp.org/t/use-destructuring-assignment-to-assign-variables-from-objects/195283 "2018-06-01T19:39:27Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanielMontilla](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/danielmontilla/32/88045_2.png) [@DanielMontilla](https://forum.freecodecamp.org/u/DanielMontilla)\
**Post date:** [June 1, 2018, 7:39pm UTC](https://forum.freecodecamp.org/t/use-destructuring-assignment-to-assign-variables-from-objects/195283/1 "2018-06-01T19:39:27Z")

</div>

**Tell us what’s happening:**

I don’t understand this at all.

Instructions:

> Consider the following ES5 code:

```auto
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

```

Ok awesome. An object with key value pairs, that we are then assigned to global variables. I know this! But then…

> Here’s the same assignment statement with ES6 destructuring syntax:

`const { x, y, z } = voxel; // x = 3.6, y = 7.4, z = 6.54`

What? Thats not at all what the previous code was. First the keys don’t have values. So x shouldn’t be 3.6. Second no global variables are being assigned to anything. This is literally just equal to this:

```auto
const Obj = {x,y,z};

```

Aka an object with declared keys (no values).  
I am so stuck. Read the ntructions like 20 times and I still have 0 clue about what I have to do.

**Link to the challenge:**

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-objects)**
>
> Learn to Code — For Free

---

<div class="post-metadata">

**Author:** ![DanielMontilla](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/danielmontilla/32/88045_2.png) [@DanielMontilla](https://forum.freecodecamp.org/u/DanielMontilla)\
**Post date:** [June 1, 2018, 10:44pm UTC](https://forum.freecodecamp.org/t/use-destructuring-assignment-to-assign-variables-from-objects/195283/2 "2018-06-01T22:44:51Z")

</div>

Ok. I took a break. Calmed downed, and read the MDN entry on Destructuring assignment. Saw some videos and I kind of got it.

Correct me if I am wrong. This ES6 feature is used to assign object values to external variables. Yes?  
Anyhow, this is my new code and its still wrong:

```auto
function getLength(str) {
  "use strict";

  // change code below this line
  const length = {x: str.length};
  const {x : len} = length; // change this
  // change code above this line

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

}

console.log(getLength('FreeCodeCamp'));

```

Says:

> destructuring with reassignment was used

with an X next to it.

---

<div class="post-metadata">

**Author:** ![alhazen1](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/alhazen1/32/85244_2.png) [@alhazen1](https://forum.freecodecamp.org/u/alhazen1)\
**Post date:** [June 2, 2018, 2:14pm UTC](https://forum.freecodecamp.org/t/use-destructuring-assignment-to-assign-variables-from-objects/195283/3 "2018-06-02T14:14:44Z")

</div>

Hi

While the solution is fairly easy the instructions are a little baffling. You are not alone in being confused by the challenge.

What they want is for you to assign the `length` property of `str` to a local variable `len` using destructuring assignment, all on one line.

This is their example that pertains to the challenge solution

```auto
const { x : a, y : b, z : c } = voxel // a = 3.6, b = 7.4, c = 6.54

//property x of voxel is assigned to local variable a

```

so you need to do  
property length of str is assigned to local variable len

---

<div class="post-metadata">

**Author:** ![DanielMontilla](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/danielmontilla/32/88045_2.png) [@DanielMontilla](https://forum.freecodecamp.org/u/DanielMontilla)\
**Post date:** [June 2, 2018, 3:19pm UTC](https://forum.freecodecamp.org/t/use-destructuring-assignment-to-assign-variables-from-objects/195283/4 "2018-06-02T15:19:35Z")

</div>

Thank you! Now I get it!

> [@alhazen1](#):
>
> property length of str is assigned to local variable len

I can see how this aught be helpful now.

---

<div class="post-metadata">

**Author:** ![aungchanmyae](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@aungchanmyae](https://forum.freecodecamp.org/u/aungchanmyae)\
**Post date:** [June 4, 2018, 8:22am UTC](https://forum.freecodecamp.org/t/use-destructuring-assignment-to-assign-variables-from-objects/195283/5 "2018-06-04T08:22:39Z")

</div>

Please show this code bro.

---

<div class="post-metadata">

**Author:** ![daultonoryan](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/daultonoryan/32/88677_2.png) [@daultonoryan](https://forum.freecodecamp.org/u/daultonoryan)\
**Post date:** [June 5, 2018, 3:31pm UTC](https://forum.freecodecamp.org/t/use-destructuring-assignment-to-assign-variables-from-objects/195283/6 "2018-06-05T15:31:20Z")

</div>

this code was super well explained if you arent getting it dot just ask for the answer track the variables and try to understand the syntax for what is going on.

---

<div class="post-metadata">

**Author:** ![patrickv77](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/patrickv77/32/91726_2.png) [@patrickv77](https://forum.freecodecamp.org/u/patrickv77)\
**Post date:** [June 14, 2018, 10:04pm UTC](https://forum.freecodecamp.org/t/use-destructuring-assignment-to-assign-variables-from-objects/195283/7 "2018-06-14T22:04:11Z")

</div>

You’re a godsend! Thanks so much for helping me understand the syntax 🙂

---

<div class="post-metadata">

**Author:** ![sebadev](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@sebadev](https://forum.freecodecamp.org/u/sebadev)\
**Post date:** [June 24, 2018, 12:26pm UTC](https://forum.freecodecamp.org/t/use-destructuring-assignment-to-assign-variables-from-objects/195283/8 "2018-06-24T12:26:37Z")

</div>

thanks! now the instructions are much easier to understand.

---

<div class="post-metadata">

**Author:** ![aditya\_p](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/aditya_p/32/99447_2.png) [@aditya\_p](https://forum.freecodecamp.org/u/aditya_p)\
**Post date:** [September 28, 2018, 7:16am UTC](https://forum.freecodecamp.org/t/use-destructuring-assignment-to-assign-variables-from-objects/195283/10 "2018-09-28T07:16:30Z")

</div>

Hey @pegofleg,  
It is always advisable to create a new topic for your problem by using the “Ask For Help” button on the challenge page.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [February 2, 2021, 11:29am UTC](https://forum.freecodecamp.org/t/use-destructuring-assignment-to-assign-variables-from-objects/195283/11 "2021-02-02T11:29:21Z")

</div>

Closed on February 2, 2021, 11:29am UTC


