Destructuring in JavaScript

Explore the destructuring assignment syntax

In this article, I am going to explain what the destructuring assignment is in JavaScript. But first, let’s see what an object property value shorthand is.

Object Property Value Shorthand

In ES6, if you have an object whose keys have the same name as the variables that are passed in as properties, you can use the object property value shorthand to simplify your code.

We can simplify the code above like this:

It’s a small feature that makes our code cleaner!

Destructuring Assignment Syntax

Destructuring assignment syntax is an expression that unpacks values from arrays and object properties into variables. It sounds strange, but it’s not! In essence, we break a structure (destructure) into individual elements, e.g., an array to array items.

Get data from an array

Let’s assume that we have an array with two elements:

And we want the value of the first element to be stored in a variable named “a” and the value of the second element to a variable named “b.”

With destructuring, we can do it like this:

In destructuring, the left part of the expression is called the target, and the right is called the source.

Keep in mind that for the destructuring to work the target and the source must have the same data structure.

With destructuring, we can easily skip items from arrays. Here’s the code:

Now the a has the value 1, and the b has the value 3. We can add as many commas as we want, but after the two commas in a row, it’s not readable.

Let’s assume that we have an array with nine items:

And we want the value of the first element to be stored to a variable named “a”, the value of the second element to a variable named “b”, and the rest of the array to a variable named “c”.

We can easily do this with the help of the reset parameter. If you don’t know what a reset parameter is, don’t worry, read this.

Swap variable values

Let’s assume that we have two variables and we want to swap their values. Here’s the code:

With destructuring, we can easily do it without the temp variable:

We created the same data structure (array) in the source and the target. It’s a simple trick for swapping.

Destruct function return value (array)

Let’s assume that we have a function that returns an array of two items:

And we want the value of the first element to be stored to a variable named “a” and the value of the second element to a variable named “b”.

With destructuring, we can simplify the above and remove the value variable:

Add default values

Let’s assume that we have an array that sometimes has two values and sometimes has one value.

We want the value of the first element to be stored to a variable named “a” and the value of the second element to a variable named “b”. Also when the array has only one value, the variable “b” will have the value “1”.

The old way to do this is with the logical operator. If you don’t know what a logical operator is, don’t worry read this.

With destructuring, we can do it like this:

Keep in mind that this works only in the case that the unpacked value is undefined or it’s not there at all. So if the value is something falsy, e.g. null, false e.t.c, it will not use the default value.

Get data from an object

Let’s assume that we have the object:

And we need two variables “name” and “email” with the values of the user object.

With destructuring, we can do it like this:

The above works perfectly because the source and the target have the same data structure. But we can use the object property value shorthand to write this in a more simplified way.

The extended way is helpful when we want the variables to have a different name than the properties, e.g., “userName”, “userEmail”:

Get data from an object with the rest parameter

Let’s assume that you have the object:

And we need a variable “name” with the user name, a variable “role” with the user role, and a variable “hobbies” with an object that contains all the user hobbies.

With destructuring, and the rest parameter we can do it like this:

Destruct function return value (object)

Let’s assume that we have a function that returns an object:

And we need a variable “name” with the user name and a variable “role” with the user role.

With destructuring, we can simplify the above and remove the value variable:

Destruct a function parameter

Let’s assume that we have this function:

We can optimize the above by destructuring the function parameter:

We created all the variables in the parameter section and now the code is more readable.

As you can see the destructuring assignment syntax in JavaScript is very powerful and can optimize/simplify our code.

This article has been published from the source ;ink without modifications to the text. Only the headline has been changed.
Source link