ES6 desctructuring - default values (#31018)
* ES6 desctructuring - default values * fix: moved array spread operator section
This commit is contained in:
@ -32,6 +32,23 @@ console.log(firstName, lastName); // John Smith
|
|||||||
|
|
||||||
The examples above show the benefit of using the ES6 Destructuring Assignment.
|
The examples above show the benefit of using the ES6 Destructuring Assignment.
|
||||||
|
|
||||||
|
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]
|
||||||
|
```
|
||||||
|
|
||||||
You can skip some values by putting comma
|
You can skip some values by putting comma
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
|
Reference in New Issue
Block a user