How does JavaScript array destructuring assign values to variables?
Quick answer
Array destructuring assigns values by position: const [first, second] = values puts the first element in first and the second in second. Leave a blank between commas to skip a position, as in const [first, , third] = values. A default such as second = "fallback" applies when that position is undefined or missing, but not when it is null. Destructuring reads values; it does not reshape the array. [S1]
The question, the details, the next step.
A practical checklist
Identify the source array and number its positions starting at the first element.
Place variables in the same order as the values you want to receive.
Use a blank slot between commas to skip a position.
Add
= defaultValuewhere a missing orundefinedvalue should get a fallback.Check whether an explicit
nullshould remainnullor needs separate handling.Confirm that the destructuring pattern reads values without changing the source array.
A few more useful answers
Does array destructuring remove the values it assigns?
No. It binds values from positions in the source; it does not remove elements or reshape the source array. [S1]
Does a destructuring default replace `null`?
No. The default is used for undefined or a missing position, not for an explicit null. [S1]
Does destructuring work only with arrays?
No. Array destructuring works with iterables, not exclusively arrays. [S1]
Sources
References used for this guide.
- [S1] Destructuring — JavaScript | MDN developer.mozilla.org · Checked
