Destructuring is a powerful ES6 feature that enables us to create variables from the elements of arrays and the properties of objects, making our JavaScript code cleaner and more readable. Let's dive deep into this concept.
Array destructuring
Array destructuring allows us to create variables from array elements. Before ES6, we used to access individual array elements using indices. With array destructuring, we use square brackets []
to create variables from array elements.
const names = ['John Doe', 'Jennifer Matlock', 'James Brock'];
const [john, jennifer, james] = names;
console.log(james); // James Brock
In this example, we've created three variables called john
, jennifer
, and james
by destructuring the names
array. Each variable corresponds to a specific array element, and we can name these variables anything we prefer.
It's important to note that variable assignment is based on the order of variables in the destructuring statement, matching them with the corresponding array elements. For example, the first variable corresponds to the first element in the array, and so on.
Skip elements
You can skip elements by inserting commas between variables:
const names = ['John Doe', 'Jennifer Matlock', 'James Brock'];
const [john, , james] = names;
console.log(james); // James Brock
In this code, we've skipped the second element in the array by placing a comma after the john
variable.
Default variable values
We can assign default values to destructured variables. So, if a variable doesn't exist in the array, the default value is used.
const names = ['John Doe', 'Jennifer Matlock'];
const [john, jennifer, james = 'James Brock'] = names;
console.log(james); // James Brock
Here, even though there are only two elements in the names
array, we've created three destructured variables and assigned a default value to the third variable james
. By doing so, james
will either correspond to the third element in the array (if it exists) or to the assigned default value 'James Brock'.
Nested array destructuring
We can destructure nested arrays, as demonstrated in the following example:
const numbers = [1, [2, 3]];
const [first, [second, third]] = numbers;
console.log(first, second, third); // 1 2 3
In this example, we've used the same array destructuring syntax []
to destructure the nested array within the numbers
array.
Using the ‘Rest’ operator
The rest operator ...
is another ES6 feature that can be used in array destructuring, as shown in this example:
const names = ['John Doe', 'Jennifer Matlock', 'James Brock'];
const [john, ...rest] = names;
console.log(rest); // [ 'Jennifer Matlock', 'James Brock' ]
In this case, we've created two variables, john
and rest
, where john
corresponds to ‘John Doe’, and rest
is an array containing the remaining array elements.
Notice that we've used the ...
syntax to create the rest
variable. You can name the rest
variable anything you prefer.
Object destructuring
Similar to array destructuring, object destructuring lets us create variables from an object's properties, leading to cleaner and more readable JavaScript code. To destructure an object, we use the curly braces {}
. Let's take a look:
const john = {
age: 30,
occupation: 'Software developer',
};
const { age, occupation } = john;
console.log(age); // 30
In this example, we've created two variables called age
and occupation
by destructuring the john
object. Notice that the variable names match the object properties, which is mandatory for object destructuring.
Rename variables
While the variable names in object destructuring have to match object properties, we can rename the variables:
const john = {
age: 30,
occupation: 'Software developer',
};
const { age, occupation: profession } = john;
console.log(profession); // software developer
Here, we've renamed the occupation
variable to profession
, and it now corresponds to ‘Software developer’. Notice that we've used the :
symbol after the variable we wanted to rename, followed by the new name, to perform this action.
Default variable values
Just like in array destructuring, we can assign default values to variables in object destructuring:
const john = {
age: 30,
occupation: 'software developer',
};
const { age, occupation: profession, salary = 5000 } = john;
console.log(salary); // 5000
In this example, we've set a default value for the salary
variable. By doing so, the salary
variable will either correspond to the salary
property in the john
object (if it exists) or the assigned default value ‘5000’.
Nested object destructuring
Similar to array destructuring, we can destructure nested objects as shown in the following example:
const john = {
age: 30,
occupation: 'Software developer',
business: {
name: 'Anetly',
address: 'Some address',
},
};
const {
age,
business: { name, address },
} = john;
console.log(address); // Some address
In this code, we've destructured the john
object, creating the age
variable, and also destructured the nested business
object, creating the name
and address
variables.
Notice that we've used the object name business
, followed by the :
symbol, and then the object destructuring syntax {}
to destructure the nested object.
Using the ‘Rest’ operator
Similar to array destructuring, we can get a portion of an object using the rest ...
operator while destructuring an object.
const john = {
age: 30,
occupation: 'Software developer',
hobby: 'Reading',
};
const { age, ...rest } = john;
console.log(rest); // { occupation: 'Software developer', hobby: 'Reading' }
In this code, we've created two variables called age
and rest
by destructuring the john
object. Here, the age
variable corresponds to the age
property, and rest
is an object containing the remaining object properties. You can name the rest
variable anything you prefer.
This concludes our discussion regarding object and array destructuring in JavaScript, highlighting their similarities and dissimilarities. The key differences are that, in array destructuring, the position of variables matters, while in object destructuring, it does not. Additionally, variable names in array destructuring can be arbitrary, whereas they must match the object properties in object destructuring.