After const { address: { city } } = user;, which variables exist?
address is just the path to city. To have both, write const { address, address: { city } } = user;.
Destructuring · Exercise 1 of 4
Fifteen multiple-choice questions on JavaScript destructuring: array and object patterns, skipping, defaults, renaming, rest, nested patterns, parameters, assignment syntax, iterables and errors. Pick an option to check your answer instantly.
After const { address: { city } } = user;, which variables exist?
address is just the path to city. To have both, write const { address, address: { city } } = user;.
Why is function init({ debug = false } = {}) written with = {}?
Without it, init() would destructure undefined and throw a TypeError. With it, the empty object is destructured and debug defaults to false.
Which value can be destructured with an array pattern?
Array patterns work with any iterable: arrays, strings, Maps, Sets, generators. Plain objects, null and numbers are not iterable.
Which line swaps the values of a and b?
The right side builds an array with the old values first, then the pattern assigns them in swapped order. (a, b = b, a is Python syntax.)
What does const [x, y] = { x: 1, y: 2 }; do?
Array patterns use the iteration protocol. Use an object pattern, const { x, y } = obj, or Object.values(obj) with an array pattern.
Timed online tests with instant scores and explanations.