DEV Community

Cover image for Avoid Writting Messy JavaScript: Learn Object Destructuring Today
Kathirvel S
Kathirvel S

Posted on

Avoid Writting Messy JavaScript: Learn Object Destructuring Today

Let’s be honest for a second.

If your JavaScript still looks like this:

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

const name = user.name;
const age = user.age;
Enter fullscreen mode Exit fullscreen mode

…we need to talk. 😅

Because JavaScript already gave us a much cleaner feature called Object Destructuring, and ignoring it is basically choosing more code for no reason.

Today we’ll learn:

  • What object destructuring is
  • How it works
  • What exactly happens behind the scenes

By the end, your code will be shorter, cleaner, and way more professional.


What is Object Destructuring?

In simple words:

Object destructuring lets you extract object properties into variables in one line.


Basic Example

const user = {
  name: "Alex",
  age: 25,
  country: "India"
};

const { name, age, country } = user;

console.log(name, age, country);
Enter fullscreen mode Exit fullscreen mode

Output

Alex 25 India
Enter fullscreen mode Exit fullscreen mode

Explanation

The line:

const { name, age, country } = user;
Enter fullscreen mode Exit fullscreen mode

means:

  • Take the name, age, and country properties from user
  • Create variables with the same names

This is much cleaner than writing three separate assignments.


Default Values

Sometimes a property may not exist. Destructuring lets you set a default value.

const user = { name: "Alex" };

const { name, age = 18 } = user;

console.log(name, age);
Enter fullscreen mode Exit fullscreen mode

Output

Alex 18
Enter fullscreen mode Exit fullscreen mode

Explanation

age = 18
Enter fullscreen mode Exit fullscreen mode

sets a default value if the object does not contain age.

This prevents undefined values in your code.


Renaming Variables

Sometimes you want the variable name to be different.

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

const { name: username, age } = user;

console.log(username, age);
Enter fullscreen mode Exit fullscreen mode

Output

Alex 25
Enter fullscreen mode Exit fullscreen mode

Explanation

name: username
Enter fullscreen mode Exit fullscreen mode

means:

  • Take the property name
  • Store it in a variable called username

The original object is not modified.


Nested Object Destructuring

Objects can contain other objects. Destructuring can extract nested values easily.

const user = {
  name: "Alex",
  address: {
    city: "Chennai",
    country: "India"
  }
};

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

console.log(city, country);
Enter fullscreen mode Exit fullscreen mode

Output

Chennai India
Enter fullscreen mode Exit fullscreen mode

Explanation

address: { city, country }
Enter fullscreen mode Exit fullscreen mode

This drills into the address object and extracts its properties.

Now city and country are top-level variables.


Destructuring in Function Parameters

Destructuring also works directly inside function parameters.

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

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

greet(user);
Enter fullscreen mode Exit fullscreen mode

Output

Hello Alex, you are 25 years old.
Enter fullscreen mode Exit fullscreen mode

Explanation

The function parameter:

{ name, age }
Enter fullscreen mode Exit fullscreen mode

automatically extracts the values from the object passed into the function.

You don’t need to write user.name inside the function.

Cleaner and easier to read.


Real-World Example: API Response

Imagine you call an API and receive this response:

const apiResponse = {
  id: 1,
  username: "devAlex",
  email: "alex@email.com",
  profile: {
    followers: 100,
    following: 50
  }
};
Enter fullscreen mode Exit fullscreen mode

You can destructure it like this:

const {
  username,
  email,
  profile: { followers, following }
} = apiResponse;

console.log(username, email, followers, following);
Enter fullscreen mode Exit fullscreen mode

Output

devAlex
alex@email.com
100
50
Enter fullscreen mode Exit fullscreen mode

Explanation

Destructuring allows you to grab:

  • username
  • email
  • nested followers
  • nested following

all in one clean statement.

No more writing:

apiResponse.profile.followers
Enter fullscreen mode Exit fullscreen mode

over and over again.


Why You Should Use Object Destructuring

  • Less code – avoid repetitive assignments
  • Better readability – easier to scan
  • Default values – prevent undefined
  • Cleaner functions – destructure parameters
  • Industry standard – widely used in React, Node.js, and API handling

Quick Tips

  • Destructure once, instead of repeatedly accessing properties.
  • Use default values when data may be missing.
  • Rename variables when needed.
  • Use destructuring inside function parameters for cleaner code.

Final Thoughts

If you are still writing this:

const name = user.name;
const age = user.age;
Enter fullscreen mode Exit fullscreen mode

…take a deep breath and embrace destructuring.

Your code will be shorter, cleaner, and much easier to maintain.

Top comments (0)