Destructuring
JavaScript · Unit 10: More on functions
- Full plan
- JavaScript
- 10 exercises
Destructuring means taking several values out of an array at once. const [a, b] = array gives the first element to a and the second to b: it goes by position.
It's like writing array[0] and array[1], but in a single line.
const top = ["Mia", "Leo", "Ann"];
const [gold, silver] = top;
console.log(gold, silver);Prints
Mia Leo
The rest of the explanation is in the lesson, which is part of the full plan.
Exercises in this lesson
You do them in the app, which checks them on the spot and explains why.
1. Predict the output
What does this code print?
2. Complete the code
Fill in the blank to split the first name from the last name and print "Stone".
3. Predict the output
What does this code print?
4. Predict the output
What does this code print?
5. Find the bug
This program should print "Leo 9". Which line has the error?
6. Multiple choice
You have
const bread = { price: 35, weight: 500 };. Which line stores 35 in a variable calledprice?7. Predict the output
In an arrow function, the destructured parameter goes inside parentheses. What does it print?
8. Complete the code
Fill in the blank so it prints "Hi, Leo".
9. Put the lines in order
Put the lines in order to print "bread: 5" and then "salt: 2".
10. Predict the output
Everything from the lesson together. What does it print?
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.