# Use-destructuring-assignment-to-assign-variables-from-arrays

**URL:** <https://forum.freecodecamp.org/t/use-destructuring-assignment-to-assign-variables-from-arrays/170377>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [January 23, 2018, 6:11am UTC](https://forum.freecodecamp.org/t/use-destructuring-assignment-to-assign-variables-from-arrays/170377 "2018-01-23T06:11:04Z")\
**Posts on this page:** 3\
**Page:** 2

<div class="post-metadata">

**Author:** ![Neveon](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/neveon/32/94677_2.png) [@Neveon](https://forum.freecodecamp.org/u/Neveon)\
**Post date:** [July 25, 2018, 2:39am UTC](https://forum.freecodecamp.org/t/use-destructuring-assignment-to-assign-variables-from-arrays/170377/23 "2018-07-25T02:39:48Z")

</div>

I believe the code at top is already in the scope for the function to read (Global Scope)

```auto
let a =8,b=6;

```

so I think re-stating the code below within the function would then make the constant variables only readable within the function. Outside the function, only the global scope of ‘let’ is readable.

```auto
const [a,b]

```

---

<div class="post-metadata">

**Author:** ![akidox](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/akidox/32/93265_2.png) [@akidox](https://forum.freecodecamp.org/u/akidox)\
**Post date:** [July 26, 2018, 11:54am UTC](https://forum.freecodecamp.org/t/use-destructuring-assignment-to-assign-variables-from-arrays/170377/24 "2018-07-26T11:54:20Z")

</div>

`const` keyword declares block level variables, the statement does not affect global variables.

---

<div class="post-metadata">

**Author:** ![bahramb92](https://avatars.discourse-cdn.com/v4/letter/b/b2d939/32.png) [@bahramb92](https://forum.freecodecamp.org/u/bahramb92)\
**Post date:** [August 12, 2018, 2:07pm UTC](https://forum.freecodecamp.org/t/use-destructuring-assignment-to-assign-variables-from-arrays/170377/25 "2018-08-12T14:07:20Z")

</div>

As i read the posts on this article , i found that most of friends are using **const** to set a variable.  
The soloution is het

You have to use :  
[a,b] = [b,a]  
Instead of :  
const [a,b] = [b,a]  
or  
Let [a,b] = [b,a]  
.

The right code is :  
let a = 8, b = 6;  
(() =\> {  
“use strict”;  
// change code below this line  
[a,b] = [b,a];  
// change code above this line  
})();  
console.log(a); // should be 6  
console.log(b); // should be 8

[Previous page](https://forum.freecodecamp.org/t/use-destructuring-assignment-to-assign-variables-from-arrays/170377.md?page=1)
