2018-10-12 15:37:13 -04:00
|
|
|
---
|
|
|
|
title: Use Destructuring Assignment to Assign Variables from Arrays
|
|
|
|
---
|
2019-07-24 00:59:27 -07:00
|
|
|
# Use Destructuring Assignment to Assign Variables from Arrays
|
2018-10-12 15:37:13 -04:00
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
|
|
|
|
---
|
|
|
|
## Hints
|
|
|
|
|
|
|
|
### Hint # 1
|
2018-10-12 15:37:13 -04:00
|
|
|
We have to take some precaution in this case.
|
|
|
|
|
|
|
|
1. No need of const [b,a] as it will keep the effect of assignment local.
|
|
|
|
|
|
|
|
2. const [b,a] = [a,b] will result in the value of a,b as undefined(simple assignment rule left to right).
|
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
|
|
|
|
---
|
|
|
|
## Solutions
|
|
|
|
|
|
|
|
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
let a = 8,
|
|
|
|
b = 6;
|
|
|
|
[a, b] = [b, a];
|
|
|
|
```
|
|
|
|
</details>
|