JavaScript 6 Destructuring Use Cases (Nested, Default) Quiz
Tackle 30 questions on nested object/array destructuring, defaults, renaming bindings, destructuring parameters, and skipping positions.
Question 1
Why is nested object destructuring useful with API responses?
Question 2
Which syntax accesses a nested property while defining a local alias?
Question 3
What logs?
const data = { stats: { points: 42 } }
const { stats: { points } } = data
console.log(points)Question 4
What happens if a middle object in a nested destructuring chain is undefined?
Question 5
What logs?
const config = { theme: { colors: { primary: '#f00' } } }
const { theme: { colors: { secondary = '#00f' } } } = config
console.log(secondary)Question 6
Why does nested array destructuring work well with coordinate tuples?
Question 7
How can you destructure a matrix to get the center item?
Question 8
What logs?
const pairs = [['A', 1], ['B', 2]]
const [ , [label, value] ] = pairs
console.log(label, value)Question 9
What happens when skipping entries with commas in array patterns?
Question 10
What logs?
const coords = [1, [2, 3]]
const [x, [y, z = 0]] = coords
console.log(x, y, z)Question 11
When do destructuring defaults apply?
Question 12
Why might you set a default for a nested property?
Question 13
What logs?
const settings = {}
const { theme: { mode = 'light' } = {} } = settings
console.log(mode)Question 14
What risk occurs when default values are expensive to compute?
Question 15
What logs?
const getUser = () => ({ name: 'Kai' })
const { name = getUser().name } = { name: undefined }
console.log(name)Question 16
What logs?
const report = { id: 10 }
const { id: reportId } = report
console.log(reportId)Question 17
Why is renaming beneficial when destructuring external data?
Question 18
Which syntax destructures while renaming two properties?
Question 19
What logs?
const info = { a: 1, b: 2 }
const { a: first = 0, c: third = 3 } = info
console.log(first, third)Question 20
What logs?
function greet({ name, role = 'guest' }) {
console.log(`${name}-${role}`)
}
greet({ name: 'Jin' })Question 21
Why is parameter destructuring helpful in utility functions?
Question 22
Which pattern guards against undefined options objects?
Question 23
What logs?
const connect = ({ host, port: p = 80 } = {}) => `${host ?? 'localhost'}:${p}`
console.log(connect({ host: 'api' }))Question 24
What logs?
const items = ['alpha', 'beta', 'gamma']
const [first, , third] = items
console.log(first, third)Question 25
Why is skipping entries useful when processing data rows?
Question 26
How do rest elements pair with skipping?
Question 27
What logs?
const nums = [10, 20, 30, 40]
const [first, , ...others] = nums
console.log(first, others)Question 28
What logs?
const [first = 'A', second = 'B'] = [undefined, null]
console.log(first, second)Question 29
Why is combining destructuring with template literals expressive?
Question 30
How does destructuring improve React/Node codebases?
