Course contentsShow
← JavaScript
Lesson 939 of 2,72321. DestructuringFree lesson

When to Use Object Destructuring

Recognize situations where destructuring improves readability versus adds complexity.

When to Use Object Destructuring

You'll learn when object destructuring improves your code and when it might make things worse.

The Core Idea

Object destructuring is a powerful tool, but like any tool, it's not always the right choice. The key is recognizing when extracting properties upfront makes your code clearer versus when it adds unnecessary complexity.

When Destructuring Shines

Use destructuring when you access properties multiple times:

// Without destructuring - repetitive
function displayUser(user) {
  console.log(user.name);
  console.log(user.email);
  return user.name + ' (' + user.email + ')';
}

// With destructuring - cleaner
function displayUser(user) {
  const { name, email } = user;
  console.log(name);
  console.log(email);
  return name + ' (' + email + ')';
}

Use it in function parameters when you need specific properties:

function greet({ name, age }) {
  return `Hello ${name}, you are ${age} years old`;
}

When to Avoid Destructuring

Don't destructure for single-use properties:

// Overkill
const { title } = book;
console.log(title);

// Simpler
console.log(book.title);

Avoid it when the object context matters:

// Context is clear
return user.address.city;

// More cryptic
const { address: { city } } = user;
return city;

The Readability Test

Ask yourself: "Does destructuring make this code easier to understand?" If you're creating variables you only use once, or if the destructuring pattern becomes deeply nested and confusing, stick with dot notation.

Key Takeaway: Use object destructuring when you access multiple properties repeatedly or in function parameters—it eliminates repetition and clarifies intent. Skip it for one-off property access where the extra syntax adds complexity rather than clarity.