Skip to main content

Command Palette

Search for a command to run...

Destructuring in JavaScript

Published
β€’5 min read
Destructuring in JavaScript
P

My name is 𝐏𝐫𝐚𝐀𝐚𝐬𝐑 and I talk about 𝗧𝗲𝗰𝗡-𝐊𝐧𝐨𝐰π₯𝐞𝐝𝐠𝐞, π—ͺπ—²π—―π——π—²π˜ƒ, π——π—²π˜ƒπ—’π—½π˜€ and π—Ÿπ—Άπ—³π—²π˜€π˜π˜†π—Ήπ—².

Introduction

When writing real-world JavaScript applications, extracting values from arrays and objects often becomes repetitive and verbose. Destructuring solves this problem by allowing you to unpack values into variables in a concise and readable way. It is one of the most useful features introduced in modern JavaScript and is widely used in frontend and backend development.

What is Destructuring

Destructuring is a syntax that allows you to extract values from arrays or properties from objects and assign them directly to variables.

Without Destructuring

const user = {
  name: "Prakash",
  age: 21,
  city: "Roorkee"
};

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

console.log(name, age, city);

With Destructuring

const user = {
  name: "Prakash",
  age: 21,
  city: "Roorkee"
};

const { name, age, city } = user;

console.log(name, age, city);

Destructuring removes repetition and makes code easier to read.

Destructuring Arrays

Array destructuring is based on position (index).

Basic Example

const numbers = [10, 20, 30];

const [a, b, c] = numbers;

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

Skipping Values

const numbers = [10, 20, 30];

const [first, , third] = numbers;

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

Using Rest Operator

const numbers = [1, 2, 3, 4, 5];

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

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

Swapping Variables

let a = 5;
let b = 10;

[a, b] = [b, a];

console.log(a); // 10
console.log(b); // 5

Destructuring Objects

Object destructuring is based on property names, not position.

Basic Example

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

const { name, age } = user;

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

Renaming Variables

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

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

console.log(userName); // Prakash
console.log(userAge);  // 21

Nested Object Destructuring

const user = {
  name: "Prakash",
  address: {
    city: "Roorkee",
    state: "Uttarakhand"
  }
};

const { address: { city, state } } = user;

console.log(city);  // Roorkee
console.log(state); // Uttarakhand

Destructuring in Function Parameters

function printUser({ name, age }) {
  console.log(`Name: \({name}, Age: \){age}`);
}

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

printUser(user);

This pattern is heavily used in React components and backend APIs.

Default Values in Destructuring

Default values help when a property or index is missing.

Object Default Values

const user = {
  name: "Prakash"
};

const { name, age = 18 } = user;

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

Array Default Values

const numbers = [10];

const [a, b = 20] = numbers;

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

Before vs After Comparison

Without Destructuring

function getUserData(user) {
  const name = user.name;
  const age = user.age;
  const city = user.city;

  console.log(name, age, city);
}

With Destructuring

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

Array Example Without

const arr = [1, 2, 3];

const first = arr[0];
const second = arr[1];

Array Example With

const [first, second] = [1, 2, 3];

Benefits of Destructuring

Destructuring reduces repetitive code and improves readability. It allows you to extract only the data you need, making functions cleaner and easier to maintain. It is especially useful when working with APIs, React props, and configuration objects. It also helps prevent errors caused by repeatedly accessing deeply nested properties.

Advanced Patterns

Nested with Default

const user = {
  name: "Prakash",
  address: {}
};

const {
  address: { city = "Unknown" }
} = user;

console.log(city); // Unknown

Rest with Objects

const user = {
  name: "Prakash",
  age: 21,
  city: "Roorkee"
};

const { name, ...rest } = user;

console.log(name); // Prakash
console.log(rest); // { age: 21, city: "Roorkee" }

Function Return Destructuring

function getData() {
  return {
    id: 1,
    title: "JavaScript"
  };
}

const { id, title } = getData();

console.log(id, title);

Mental Model

For objects, destructuring works by matching property names to variables. For arrays, destructuring works based on index positions. Understanding this difference helps avoid most common mistakes.

Common Mistakes

Wrong Property Name

const user = { name: "Prakash" };

const { username } = user; // undefined

Unsafe Nested Destructuring

const user = {};

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

Safe Fix

const { address: { city } = {} } = user;

Conclusion

Destructuring is not just a syntax improvement but a better way of writing JavaScript. It simplifies data extraction, reduces code repetition, and makes your logic more expressive. Once you start using it consistently, it becomes a natural part of how you write modern JavaScript.


❊ Want More…?

I write articles on blog.prakashtsx.com and also post development-related content on the following platforms:

JavaScript

Part 4 of 15

In this series of blogs I have written some interesting concepts related to JavaScript.

Up next

Array Flattening in JavaScript

Understand Array Flattening in JavaScript

More from this blog