2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								title: Destructuring Assignment
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Destructuring Assignment
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Destructuring Assignment syntax is a Javascript expression that makes it possible to unpack values or properties from arrays or objects.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-11-07 15:34:13 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Lets say that you have an array that contains a first name and last name as its two values, but you want to reassign those values to something more descriptive. You can use Destructuring to accomplish this.
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**ES5 Destructuring**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								var fullName = ["John", "Smith"];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								var firstName = fullName[0];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								var lastName = fullName[1];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								console.log(firstName, lastName); // John Smith
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**ES6 Destructuring**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const fullName = ["John", "Smith"];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const [firstName, lastName] = fullName;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								console.log(firstName, lastName); // John Smith
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The examples above show the benefit of using the ES6 Destructuring Assignment.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-02-18 05:03:50 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								A variable can be assigned a default, in the case that the value unpacked from the array is undefined.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```javascript
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const address = ['Rue Cler', 'Paris'];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[street, city, country = 'France'] = address;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								console.log(country); // 'France'
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Array Destructuring with rest**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								When destructuring an array, you can unpack and assign the remaining part of it to a variable using the rest pattern:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const [a, ...b] = [1, 2, 3];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								console.log(a); // 1
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								console.log(b); // [2, 3]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-02-18 00:37:56 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								You can skip some values by putting comma
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```javascript
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const address = ['Rue Cler', 'Paris', 'France'];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const [street, , country] = address;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								or
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const [street] = address;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								You can also use Destructuring on objects using a similar syntax
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2018-12-07 04:27:24 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const fullName = {first: "John", last: "Smith"};
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								const {first, last} = fullName;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								console.log(first, last); // John Smith
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Object Destructuring Assignment is a little bit different because the object must have properties with the names you are assigning. Therefore the code below would not work as intended.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2018-12-07 04:27:24 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const fullName = {first: "John", last: "Smith"};
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								const {firstName, lastName} = fullName;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								console.log(firstName, lastName); // undefined undefined
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								You can still achieve the desired result using the following syntax.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const obj = {propertyName: value}
							 
						 
					
						
							
								
									
										
										
										
											2018-10-16 09:21:09 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const {propertyName: desiredVariableName} = obj
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Our previous example would be rewritten as follows:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2018-12-07 04:27:24 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const fullName = {first: "John", last: "Smith"};
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								const {first: firstName, last: lastName} = fullName;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								console.log(firstName, lastName); // John Smith
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-12-07 04:27:24 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-02-18 05:21:46 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								A variable can be assigned a default, in the case that the value unpacked from the object is undefined.
							 
						 
					
						
							
								
									
										
										
										
											2018-12-07 04:27:24 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2019-02-18 05:21:46 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const person = {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  age: 22
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
									
										
										
										
											2018-12-07 04:27:24 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-02-18 05:21:46 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const {name: firstName = 'Anonymous', age} = person;
							 
						 
					
						
							
								
									
										
										
										
											2018-12-07 04:27:24 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-02-18 05:21:46 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Destructuring an array with rest, you can unpack and assign the remaining part of it to a variable using the rest pattern:
							 
						 
					
						
							
								
									
										
										
										
											2018-10-16 09:21:09 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const [a, ...b] = [1, 2, 3];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								console.log(a); // 1
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								console.log(b); // [2, 3]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```