Destructure and rename variables with JavaScript, plus other cool JS destructuring use cases
Last updated on:
Table of Contents
Table of Contents
Want to simplify your JavaScript code and extract the values you need from objects and arrays easily? Learn how to use destructuring! In this article, I'll show you how to destructure objects and arrays, rename variables, set default values, and merge arrays and objects. Plus, I'll also cover how to use destructuring in practical scenarios, such as extracting data from APIs and handling function parameters.
Destructuring and Renaming Variables in JavaScript
Destructuring is a useful feature in JavaScript which lets you assign variables to the values of object properties and array items easily. It is a way of unpacking data from objects and arrays into different variables.
This allows you to create new variables from object properties and array items. In JavaScript, you can destructure objects with curly braces and arrays with brackets [].
JavaScript Object Destructuring Syntax
Destructuring can be used in lots of ways, but the most common usage is to extract properties from an object or items from an array.
For objects, the syntax is as follows:
Here, we’re destructuring the person
object and assigning its name
and age
property values to a name
and age
variable.
Note: Destructuring doesn’t actually change the original object at all. It only copies the value of the object properties.
JavaScript Array Destructuring Syntax
For arrays, the syntax is:
In the example above, we use destructuring to assign the first two items from the numbers
array to the first
and second
variables.
Storing the remaining values in a separate variable
You can use the ...
rest operator to assign the remaining properties or items in an array to another variable.
Whatever variable you put the ...
rest operator in front of, it will have the rest of the values that you didn’t destructure. It will create a new object when destructuring an object and a new array when destructuring an array.
Renaming variables using destructuring
One of the useful features of destructuring is that it allows you to rename variables during the assignment. You can use the colon :
to rename a variable.
For example:
In this example, we are renaming the name
property to personName
and the age
property to personAge
. This is useful when you want to rename a property to something more specific or if there is another variable in the current JavaScript scope with the same name.
Note: the right hand-side will be the new name that you want to use, and the left hand-side will be the name of the property you want to destructure.
{name: firstName} means {name as firstName}
Default values with destructuring
You can also set default values for variables in case the value is undefined or null. You can do this by using the equal sign =
after the variable name.
For example:
In this example, if the age
property is not defined in the person
object, it will default to 30
. This feature is particularly useful when you need to handle optional properties in an object.
Practical examples of destructuring in JavaScript
Destructuring can be used in many practical scenarios such as extracting data from APIs, handling function parameters, and more. Here are a few cool tricks you can do with destructuring in JavaScript.
Extracting function arguments
You can use destructuring to quickly create variables from an object parameter for a function.
In this example, we are using destructuring to extract the firstName
and lastName
properties from the person
object to get the full name. This makes the code more readable, and the function parameter is self-documenting.
Overall, destructuring and renaming variables in JavaScript is a powerful feature that can simplify your code and make it more readable. With destructuring, you can extract the values you need from objects and arrays with ease, and with renaming, you can make your code more descriptive and self-documenting.
Removing some properties from an object
To remove some properties from an object, you can use destructuring and the rest operator.
In this example, we are removing the age
property from the person
object and creating a new object without it. This can be useful when you want to pass an object to a function, but don’t want to include certain properties.
Merging arrays and objects
Destructuring is also useful when you want to merge objects or arrays. You can use the spread operator to merge objects or arrays together.
Here, we are using the spread operator to merge the person
and address
objects into a new object. We are also using the spread operator to merge two arrays into a new array. This is a concise way of merging objects and arrays without having to write a lot of code.
Multiple return values
Finally, destructuring can be used to return multiple values from a function.
In this example, we are using destructuring to assign the values returned from the getValues
function to the variables a
, b
, and c
. This is a convenient way to return multiple values from a function without having to pack them into an object or array.
If you are using react, then this syntax will be familiar. It is used to destructure the return values of useState
and other react hooks.
Here’s an example of using useState
with destructuring in a React component:
Summary
JavaScript destructuring allows for easy assignment of object properties and array items to variables. Renaming variables, setting default values, and merging arrays and objects are all possible with destructuring. It can be used in practical scenarios such as extracting data from APIs and handling function parameters. Destructuring is also used in React hooks such as useState
. What are your favourite ways of using destructuring? Let me know in the comments below.
Get the latest posts & updates directly to your inbox
No span, ever. Unsubscribe at any time
Subscibe to my newsletter