2018-09-30 22:01:58 +00:00
---
id: 587d7b89367417b2b2512b49
title: Use Destructuring Assignment to Assign Variables from Objects
challengeType: 1
2019-08-05 16:17:33 +00:00
forumTopicId: 301215
2021-01-13 02:31:00 +00:00
dashedName: use-destructuring-assignment-to-assign-variables-from-objects
2018-09-30 22:01:58 +00:00
---
2020-11-27 18:02:05 +00:00
# --description--
2019-06-20 14:28:46 +00:00
Destructuring allows you to assign a new variable name when extracting values. You can do this by putting the new name after a colon when assigning the value.
2019-05-17 13:20:30 +00:00
2019-06-07 15:19:03 +00:00
Using the same object from the last example:
2019-05-17 13:20:30 +00:00
```js
2019-06-07 15:19:03 +00:00
const user = { name: 'John Doe', age: 34 };
2019-05-17 13:20:30 +00:00
```
2019-06-07 15:19:03 +00:00
Here's how you can give new variable names in the assignment:
2019-05-17 13:20:30 +00:00
```js
2019-06-07 15:19:03 +00:00
const { name: userName, age: userAge } = user;
2019-05-17 13:20:30 +00:00
```
2021-03-03 00:12:12 +00:00
You may read it as "get the value of `user.name` and assign it to a new variable named `userName` " and so on. The value of `userName` would be the string `John Doe` , and the value of `userAge` would be the number `34` .
2020-11-27 18:02:05 +00:00
# --instructions--
Replace the two assignments with an equivalent destructuring assignment. It should still assign the variables `highToday` and `highTomorrow` the values of `today` and `tomorrow` from the `HIGH_TEMPERATURES` object.
# --hints--
You should remove the ES5 assignment syntax.
```js
assert(
!code.match(/highToday = HIGH_TEMPERATURES\.today/g) &&
!code.match(/highTomorrow = HIGH_TEMPERATURES\.tomorrow/g)
);
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
You should use destructuring to create the `highToday` variable.
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
assert(
code.match(
/(var|const|let)\s*{\s*(today\s*:\s*highToday[^}]*|[^,]*,\s*today\s*:\s*highToday\s*)}\s*=\s*HIGH_TEMPERATURES(;|\s+|\/\/)/g
)
);
```
You should use destructuring to create the `highTomorrow` variable.
```js
assert(
code.match(
/(var|const|let)\s*{\s*(tomorrow\s*:\s*highTomorrow[^}]*|[^,]*,\s*tomorrow\s*:\s*highTomorrow\s*)}\s*=\s*HIGH_TEMPERATURES(;|\s+|\/\/)/g
)
);
```
`highToday` should be equal to `77` and `highTomorrow` should be equal to `80` .
```js
assert(highToday === 77 & & highTomorrow === 80);
```
# --seed--
## --seed-contents--
2018-09-30 22:01:58 +00:00
```js
2019-06-07 15:19:03 +00:00
const HIGH_TEMPERATURES = {
yesterday: 75,
today: 77,
tomorrow: 80
2018-09-30 22:01:58 +00:00
};
2020-03-04 19:08:54 +00:00
// Only change code below this line
2019-06-07 15:19:03 +00:00
const highToday = HIGH_TEMPERATURES.today;
const highTomorrow = HIGH_TEMPERATURES.tomorrow;
2018-09-30 22:01:58 +00:00
2020-03-04 19:08:54 +00:00
// Only change code above this line
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
# --solutions--
2018-09-30 22:01:58 +00:00
```js
2019-06-07 15:19:03 +00:00
const HIGH_TEMPERATURES = {
yesterday: 75,
today: 77,
tomorrow: 80
2018-10-14 06:02:55 +00:00
};
2019-06-07 15:19:03 +00:00
const { today: highToday, tomorrow: highTomorrow } = HIGH_TEMPERATURES;
2018-09-30 22:01:58 +00:00
```