Destructuring lets you unpack values from objects and arrays into separate variables in a single line. Instead of writing user.name, user.email, user.role over and over, you extract them once.
Copy
const user = { name: "Sarah", email: "sarah@example.com", role: "admin" };// ❌ Without destructuring — repetitiveconst name = user.name;const email = user.email;const role = user.role;// ✅ With destructuring — one lineconst { name, email, role } = user;console.log(name); // "Sarah"console.log(email); // "sarah@example.com"console.log(role); // "admin"
Pull specific properties out of an object by name:
Python mental model: object destructuring is like doing multiple dict lookups and assignments in one step (name = user["name"], email = user["email"]) — but with a much shorter syntax.
Copy
const product = { id: 42, name: "Wireless Headphones", price: 79.99, category: "Electronics", inStock: true,};// Grab only what you needconst { name, price, inStock } = product;console.log(name); // "Wireless Headphones"console.log(price); // 79.99console.log(inStock); // true// id and category are ignored
Set fallbacks for properties that might not exist:
Copy
const settings = { theme: "dark" };const { theme, language = "en", fontSize = 14 } = settings;console.log(theme); // "dark" (from the object)console.log(language); // "en" (default — not in object)console.log(fontSize); // 14 (default — not in object)
Python mental model: this is similar to settings.get("language", "en"). One key difference: destructuring defaults only apply when the property is undefined (missing). If the value is explicitly null, the default is not used.
Don’t go more than two levels deep with nested destructuring. It quickly becomes unreadable. For deeply nested data, destructure in multiple steps or use dot notation.
Python mental model: this is like tuple unpacking (first, second = values) — but JavaScript adds useful extras like skipping items and collecting “the rest” with ...remaining.
// Extract what you need in the parameter listfunction displayOrder({ id, customer, total }) { console.log(`Order ${id} for ${customer}: $${total}`);}displayOrder({ id: "ORD-001", customer: "Sarah", total: 149.99, items: [...] });// "Order ORD-001 for Sarah: $149.99"
If you’re coming from Python, this feels like writing def display_order(order): ... and immediately doing id = order["id"], customer = order["customer"], total = order["total"] at the top of the function.