Skip to content

Commit a82f41f

Browse files
authored
Added Destructuring example to “Destructuring assignment” doc (mdn#5145)
Relates to mdn#4682
1 parent d0cbed6 commit a82f41f

File tree

1 file changed

+10
-0
lines changed
  • files/en-us/web/javascript/reference/operators/destructuring_assignment

1 file changed

+10
-0
lines changed

files/en-us/web/javascript/reference/operators/destructuring_assignment/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ <h4 id="Assignment_separate_from_declaration">Assignment separate from declarati
9191
console.log(b); // 2
9292
</pre>
9393

94+
<p>In an array destructuring from an array of length <var>N</var> specified on the right-hand side of the assignment, if the number of variables specified on the left-hand side of the assignment is greater than <var>N</var>, only the first <var>N</var> variables are assigned values. The values of the remaining variables will be undefined.</p>
95+
<pre class="brush: js">const foo = ['one', 'two'];
96+
97+
const [red, yellow, green, blue] = foo;
98+
console.log(red); // "one"
99+
console.log(yellow); // "two"
100+
console.log(green); // undefined
101+
console.log(blue); //undefined
102+
</pre>
103+
94104
<h4 id="Default_values">Default values</h4>
95105

96106
<p>A variable can be assigned a default, in the case that the value unpacked from the array is <code>undefined</code>.</p>

0 commit comments

Comments
 (0)