Skip to main content

Command Palette

Search for a command to run...

Destructuring in JavaScript

Updated
β€’4 min read

In JavaScript, working with arrays and objects often involves extracting values. Traditionally, this required repetitive code.

Destructuring solves this problem by allowing you to unpack values from arrays or objects into variables in a clean and concise way.


πŸ”Ή What is Destructuring?

πŸ‘‰ Destructuring = breaking down complex data structures (arrays/objects) into smaller variables

Instead of accessing values one by one, you can extract them in a single line.


πŸ”Ή Before vs After Destructuring

❌ Without Destructuring

const user = {
  name: "Sayantan",
  age: 21
};

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

βœ… With Destructuring

const user = {
  name: "Sayantan",
  age: 21
};

const { name, age } = user;

βœ” Less code βœ” Cleaner βœ” More readable


πŸ”Ή Destructuring Arrays

Array destructuring is based on position (index).

πŸ“Œ Example

const arr = [10, 20, 30];

const [a, b, c] = arr;

console.log(a); // 10
console.log(b); // 20
console.log(c); // 30

πŸ”Ή Skipping Values

const arr = [10, 20, 30];

const [first, , third] = arr;

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

πŸ”Ή Using Rest Operator

const arr = [1, 2, 3, 4];

const [first, ...rest] = arr;

console.log(first); // 1
console.log(rest);  // [2, 3, 4]

πŸ”Ή Destructuring Objects

Object destructuring is based on property names, not position.

πŸ“Œ Example

const user = {
  name: "Sayantan",
  age: 21,
  city: "Kolkata"
};

const { name, age } = user;

console.log(name); // Sayantan
console.log(age);  // 21

πŸ”Ή Renaming Variables

const user = {
  name: "Sayantan"
};

const { name: userName } = user;

console.log(userName); // Sayantan

πŸ”Ή Nested Destructuring

const user = {
  name: "Sayantan",
  address: {
    city: "Kolkata"
  }
};

const {
  address: { city }
} = user;

console.log(city); // Kolkata

πŸ”Ή Default Values

If a value is missing, you can assign a default.

const user = {
  name: "Sayantan"
};

const { name, age = 18 } = user;

console.log(name); // Sayantan
console.log(age);  // 18

πŸ”Ή Destructuring in Function Parameters

function greet({ name, age }) {
  console.log(`Hello \({name}, age \){age}`);
}

greet({ name: "Sayantan", age: 21 });

βœ” Very common in real-world code (especially React)


πŸ”Ή Benefits of Destructuring

βœ… 1. Reduces Repetitive Code

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

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

βœ… 2. Improves Readability

Cleaner and easier to understand structure.


βœ… 3. Makes Code More Expressive

You clearly see what data is being used.


βœ… 4. Useful in Modern JavaScript

  • React props

  • API responses

  • Function arguments


πŸ”Ή Real-World Example

const response = {
  data: {
    user: {
      name: "Sayantan",
      age: 21
    }
  }
};

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

console.log(name, age);

⚠️ Common Mistakes

❌ Wrong Variable Names

const user = { name: "Sayantan" };

const { username } = user; // ❌ undefined

βœ” Must match property name


❌ Accessing Nested Without Safety

const user = {};

const {
  address: { city }
} = user; // ❌ error

βœ” Use optional chaining or defaults


🧠 Mental Model

  • Array destructuring β†’ position-based

  • Object destructuring β†’ key-based

More from this blog