Skip to main content

Command Palette

Search for a command to run...

Destructuring in JavaScript ⚑

Writing Cleaner and More Readable Code

Updated
β€’4 min read
Destructuring in JavaScript ⚑

πŸ“Œ Introduction

In JavaScript, we often work with arrays and objects.

But extracting values from them used to look repetitive like this:

πŸ‘‰ Too many lines πŸ‘‰ Too much repetition πŸ‘‰ Hard to read

To solve this, JavaScript introduced destructuring.


🧠 What is Destructuring?

Destructuring is:

A way to extract values from arrays or objects into variables easily.


🧩 Simple Meaning:

πŸ‘‰ β€œUnpack data into variables”


πŸ“¦ Array Destructuring


❌ Before Destructuring:

const numbers = [10, 20, 30];

const first = numbers[0];
const second = numbers[1];
const third = numbers[2];

console.log(first, second, third);

βœ… After Destructuring:

const numbers = [10, 20, 30];

const [first, second, third] = numbers;

console.log(first, second, third);

🧠 What happened?

[10, 20, 30]
   ↓
first = 10
second = 20
third = 30

⚑ Skipping Values

You can skip elements:

const numbers = [10, 20, 30];

const [first, , third] = numbers;

console.log(first, third);

🧩 Object Destructuring


❌ Before Destructuring:

const user = {
  name: "Tony",
  age: 40,
  city: "New York"
};

const name = user.name;
const age = user.age;
const city = user.city;

console.log(name, age, city);

βœ… After Destructuring:

const user = {
  name: "Tony",
  age: 40,
  city: "New York"
};

const { name, age, city } = user;

console.log(name, age, city);

🧠 Visual:

user object
   ↓
name β†’ "Tony"
age  β†’ 40
city β†’ "New York"

πŸ” Renaming Variables

You can rename properties:

const user = {
  name: "Peter",
  age: 25
};

const { name: userName, age: userAge } = user;

console.log(userName, userAge);

🧠 Default Values

If a value is missing, you can set defaults.


πŸ“¦ Array Example:

const numbers = [10];

const [first, second = 20] = numbers;

console.log(first, second);

πŸ“¦ Object Example:

const user = {
  name: "Bruce"
};

const { name, age = 30 } = user;

console.log(name, age);

πŸš€ Why Destructuring is Useful


1️⃣ Less Repetition

πŸ‘‰ No need to write object.property again and again


2️⃣ Cleaner Code

// Before
const name = user.name;
const age = user.age;

// After
const { name, age } = user;

3️⃣ Better Readability

πŸ‘‰ Code becomes easier to understand at a glance


4️⃣ Useful in Function Parameters

function printUser({ name, age }) {
  console.log(name, age);
}

🌍 Real-World Example


πŸ“‘ API Response:

const response = {
  data: {
    user: {
      name: "Steve",
      age: 35
    }
  }
};

🧩 Without Destructuring:

const name = response.data.user.name;

⚑ With Destructuring:

const {
  data: {
    user: { name }
  }
} = response;

console.log(name);

πŸ”„ Array vs Object Destructuring

Feature Array Object
Based on Position Keys
Order matters Yes No
Syntax [a, b] {a, b}

🎯 Key Takeaways

  • Destructuring extracts values easily

  • Works with arrays and objects

  • Reduces repetitive code

  • Supports default values and renaming

  • Improves readability


✍️ Practice Challenge

Try this:

πŸ‘‰ Create an object with:

  • name

  • age

  • city

Then destructure it and print values.


πŸ”₯ Conclusion

Destructuring is a small feature with a big impact.

πŸ‘‰ Less code πŸ‘‰ Cleaner structure πŸ‘‰ Better readability

Once you start using it, your JavaScript becomes much more elegant and professional πŸ’ͺ


Happy Coding ⚑✨

How the Web Works πŸ•ΈοΈπŸ•ΈοΈ

Part 7 of 50

A practical web development series explaining how the web worksβ€”from DNS and browsers to servers, HTTP, APIs, and deploymentβ€”while clearly connecting these fundamentals to real-world website programming using frontend and backend examples.

Up next

What is Middleware in Express and How It Works βš™οΈ

Understanding the Request Pipeline in Node.js