Sitemap
Troy Gooden

Troy Gooden

Simplifying Code with Object Destructuring in ES6

3 min readMar 23, 2020

--

One of my favorite features of ES6 is object destructuring, a technique that simplifies not only the way your code looks but also how easily you can access your data. In essence, destructuring allows you to extract and assign variables from small to large datasets with minimal effort. In JavaScript, destructuring is commonly used with arrays and objects.

Destructuring Arrays

Press enter or click to view image in full size

Traditionally, when accessing elements from an array like names or colors, you would use bracket notation to specify the index of the value you need. While this method works, what if I told you there’s a cleaner, more intuitive way to achieve the same result? Enter array destructuring.

Press enter or click to view image in full size

In the example above, we use bracket notation on the left side of the equal sign to define variables. These variables directly correlate to the indexes of the array on the right side of the equal sign. For instance, nameOne is assigned the value ‘Mark’ because it corresponds to the first index of the names array. Every variable on the left is mapped to the respective index in the array.

Although it may seem complex at first, playing around with destructuring quickly reveals its elegance and power. With array destructuring, you can extract multiple values in a concise, readable way, reducing redundancy and improving clarity in your code.

Destructuring Objects

Get Troy Gooden’s stories in your inbox

Join Medium for free to get updates from this writer.

Where destructuring truly shines is with objects, especially when working with frameworks like React. In scenarios where you’re passing down large amounts of data through props, destructuring allows you to explicitly pick out the properties you need, saving time and making your code more manageable.

Unlike arrays, destructuring objects works by matching keys to variable names, not indexes. This provides a precise way to access only the data you need without having to repeatedly reference the object itself.

Press enter or click to view image in full size

In the example above, we declare a constant on the left-hand side with the object keys we want to extract (name, street, city etc). By placing the object business on the right side of the equal sign, we can directly assign these values to variables without having to write repetitive code like business.name or business.street.

Why Use Destructuring?

Destructuring offers several advantages:

1. Cleaner Code: By reducing repetitive access patterns, it makes your code more concise and readable.

2. Explicit Variable Selection: You can extract only the data you need, avoiding unnecessary clutter.

3. Syntactic Sugar: Destructuring adds elegance and simplicity, making your code easier to write and understand.

Whether you’re simplifying variable naming or cleaning up your codebase, destructuring is a powerful tool that deserves a place in your JavaScript toolkit. Play around with it, integrate it into your projects, and see how it transforms your coding experience.

--

--