Lesson 13 of 80
Pulling values out of arrays and objects by matching a pattern, instead of reaching in with indexes and dot-paths one at a time.
10 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
Question 1 of 2
Given `const { count = 5 } = { count: 0 };`, what is `count`?
Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
Destructuring lets you unpack values from an array or object straight into variables by writing a pattern that mirrors the shape you're pulling from.
const user = { name: "Ada", address: { city: "London" } };
const { name, address: { city } } = user;
// name -> "Ada", city -> "London"
const [first, , third] = [10, 20, 30];
// first -> 10, third -> 30 (the middle slot is skipped)
function greet({ name, greeting = "Hi" }) {
return `${greeting}, ${name}`;
}
Object destructuring matches by key name (order doesn't matter); array destructuring matches by position (order is everything). Both support default values for when the source is undefined, and both can rename: const { name: userName } = user.
const user = { name: 'Ada', address: { city: 'London' } } — the real object.
undefined, not on other falsy values — const { count = 5 } = { count: 0 } gives you 0, not 5, because 0 isn't undefined.null or undefined throws. const { x } = null is a TypeError — there's nothing to read a property from. Guard with a default parameter (= {}) if the source might be missing.const { name: userName = "Anonymous" } = user reads user.name, calls it userName locally, and falls back to "Anonymous" if user.name is undefined.Destructuring shows up constantly in real code review and in "what does this log" snippets involving defaults, renaming, and nested patterns — it's less a standalone topic than a fluency check on reading everyday modern JS.