2018-09-30 23:01:58 +01:00
---
id: 587d7b89367417b2b2512b4b
title: Use Destructuring Assignment to Assign Variables from Arrays
challengeType: 1
2019-08-05 09:17:33 -07:00
forumTopicId: 301213
2021-01-13 03:31:00 +01:00
dashedName: use-destructuring-assignment-to-assign-variables-from-arrays
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
2018-09-30 23:01:58 +01:00
ES6 makes destructuring arrays as easy as destructuring objects.
2020-11-27 19:02:05 +01:00
2018-09-30 23:01:58 +01:00
One key difference between the spread operator and array destructuring is that the spread operator unpacks all contents of an array into a comma-separated list. Consequently, you cannot pick or choose which elements you want to assign to variables.
2020-11-27 19:02:05 +01:00
2018-09-30 23:01:58 +01:00
Destructuring an array lets us do exactly that:
2019-05-17 06:20:30 -07:00
```js
const [a, b] = [1, 2, 3, 4, 5, 6];
2021-03-02 16:12:12 -08:00
console.log(a, b);
2019-05-17 06:20:30 -07:00
```
2021-03-02 16:12:12 -08:00
The console will display the values of `a` and `b` as `1, 2` .
2020-11-27 19:02:05 +01:00
The variable `a` is assigned the first value of the array, and `b` is assigned the second value of the array. We can also access the value at any index in an array with destructuring by using commas to reach the desired index:
2019-05-17 06:20:30 -07:00
```js
const [a, b,,, c] = [1, 2, 3, 4, 5, 6];
2021-03-02 16:12:12 -08:00
console.log(a, b, c);
2019-05-17 06:20:30 -07:00
```
2021-03-02 16:12:12 -08:00
The console will display the values of `a` , `b` , and `c` as `1, 2, 5` .
2020-11-27 19:02:05 +01:00
# --instructions--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
Use destructuring assignment to swap the values of `a` and `b` so that `a` receives the value stored in `b` , and `b` receives the value stored in `a` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --hints--
2018-09-30 23:01:58 +01:00
2021-03-02 16:12:12 -08:00
The value of `a` should be `6` , after swapping.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(a === 6);
2018-09-30 23:01:58 +01:00
```
2021-03-02 16:12:12 -08:00
The value of `b` should be `8` , after swapping.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(b === 8);
```
2018-09-30 23:01:58 +01:00
2021-03-02 16:12:12 -08:00
You should use array destructuring to swap `a` and `b` .
2018-09-30 23:01:58 +01:00
```js
2020-11-27 19:02:05 +01:00
assert(/\[\s*(\w)\s*,\s*(\w)\s*\]\s*=\s*\[\s*\2\s*,\s*\1\s*\]/g.test(code));
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
# --seed--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
## --seed-contents--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
let a = 8, b = 6;
// Only change code below this line
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
2019-02-25 12:57:50 -05:00
let a = 8, b = 6;
2019-03-25 19:49:34 +05:30
[a, b] = [b, a];
2018-09-30 23:01:58 +01:00
```