Skip to main content

Command Palette

Search for a command to run...

JavaScript Destructuring Explained (In Simple Way 😎)

Published
5 min read
JavaScript Destructuring Explained (In Simple Way 😎)

Alright… today we are going to talk about something that every JavaScript developer uses… but many don’t truly feel it.

Destructuring.

And trust me… once you understand this properly, your code will go from:

“Bhai thoda lamba hai…”

to

“Arre waah… clean code 😎”

Also, this is one of those features that makes you look like a pro developer in front of juniors.

So let’s break it down… in our style.


So, what does Destructuring mean?

Well technically!!!…

Destructuring is a way to unpack values from arrays or objects into separate variables.

Too technical again 😑

Let’s understand in our own way…

👉 Destructuring means: taking data out of a structure (array/object) and directly putting it into variables.

Simple.


The Problem (Before Destructuring)

Let’s say you have this object:

const user = {
  name: "Barun",
  age: 22,
  city: "Patna"
};

Now if you want to use these values:

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

console.log(name, age, city);

Works fine…

But imagine 10 properties…

or nested objects…

or APIs…

👉 You will cry 😭


Enter Destructuring 🚀

const user = {
  name: "Barun",
  age: 22,
  city: "Patna"
};

const { name, age, city } = user;

console.log(name, age, city);

बस्स्स्स… done.

No repetition. No headache.


Destructuring Arrays

Let’s start with arrays first.

const songs = ["Saiyaara", "Tum Hi Ho", "Kesariya"];

Without destructuring:

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

With destructuring:

const [first, second, third] = songs;

console.log(first);  // Saiyaara
console.log(second); // Tum Hi Ho

👉 See the difference?

No indexing. No confusion.


Real-Life Example 🎧

Imagine your playlist:

const playlist = ["Workout Song", "Focus Song", "Sleep Song"];
const [workout, focus, sleep] = playlist;

Now your variables make sense.


Skipping Values in Arrays

const numbers = [10, 20, 30, 40];

const [first, , third] = numbers;

console.log(first); // 10
console.log(third); // 30

👉 That empty comma means: “ignore this value”


Destructuring Objects

Now comes the real power 💥

const student = {
  name: "Rahul",
  marks: 90,
  subject: "Maths"
};

Without destructuring:

const name = student.name;
const marks = student.marks;

With destructuring:

const { name, marks } = student;

console.log(name);  // Rahul
console.log(marks); // 90

Important Rule ⚠️

👉 Variable name must match the key name

const { name } = student; // correct

If you want a different name:

const { name: studentName } = student;

console.log(studentName);

Default Values

Now comes a lifesaver feature 🙏

const user = {
  name: "Barun"
};

Now if you try:

const { name, age } = user;

console.log(age); // undefined

But with default value:

const { name, age = 18 } = user;

console.log(age); // 18

👉 Useful when API doesn’t send data.


Real-world Scenario 🌍

const apiResponse = {
  username: "coder123"
};

const { username, isAdmin = false } = apiResponse;

console.log(isAdmin); // false

👉 No crash. Safe code.


Before vs After (The Real Truth)

Before 😵

const product = {
  title: "Laptop",
  price: 50000,
  brand: "HP"
};

const title = product.title;
const price = product.price;
const brand = product.brand;

After 😎

const { title, price, brand } = product;

👉 Cleaner 👉 Faster 👉 Less repetitive


Why Destructuring is Powerful?

Let me tell you like a senior developer would…

1. Less Repetition

No more object.property again and again.

2. Cleaner Code

Your code looks readable and professional.

3. Easy to Work with APIs

Most APIs return objects → destructuring saves time.

4. Better Naming

You can rename variables easily.

5. Default Safety

No more undefined errors.


One Last Example (Interview Style 💀)

const user = {
  name: "Barun",
  address: {
    city: "Patna"
  }
};

const { address: { city } } = user;

console.log(city); // Patna

👉 Nested destructuring… next level 🔥


Final Thoughts

Destructuring is not just a feature…

👉 It’s a habit.

Once you start using it, you won’t go back.

And trust me…

When you stop writing things like:

user.name
user.age
user.city

and start writing:

const { name, age, city } = user;

👉 You officially enter clean code territory.


Your Turn (Don’t Run Away 😏)

Q1

const marks = [50, 60, 70];

Destructure and print all values.


Q2

const student = {
  name: "Aman",
  age: 20
};

Extract name and give default value to city.


Q3

Fix this:

const user = { name: "Barun" };
const { age } = user;
console.log(age);

Try these… and don’t cheat 😄


And yes…

If you understood this properly…

👉 You are already ahead of 60% beginners.

बाकी 40% अभी भी user.name लिख रहे हैं 😏


Happy Coding 🚀 Keep Learning