JavaScriptFundamental (ES6), part 1
Array destructuring in JavaScript
16 Array destructuring by position
easyPull values out of an array by position: two names, a skipped slot, more names than there are values. Then try the same pattern on a string, on a Set, and on an object that only looks like an array.
Stuck? Three hints, smallest first
- 1. An array pattern matches by position, so
const [a, b] = listtakes index 0 and index 1. - 2. A bare comma skips a position. Two commas skip two.
- 3. The pattern uses the iterator protocol, not indexes — so anything iterable works, and anything not iterable throws.
const attempt = (fn) => {
try { return fn(); } catch (e) { return e.constructor.name; }
};
const basket = ['rice', 'beans', 'yam'];
const [first, second] = basket;
const [, middle] = basket;
const [one, two, three] = ['rice', 'beans']; // three names, two values
const [letterA, letterB] = 'hi'; // a string is iterable
const [smallest, next] = new Set([10, 20, 30]); // so is a Set
const arrayLike = { 0: 'zero', length: 1 }; // looks like an array, is not iterable
const fromArrayLike = attempt(() => {
const [only] = arrayLike;
return only;
});
// The pattern pulls only as many values as it has slots.
const pulled = [];
function* counted() {
for (const n of [1, 2, 3, 4, 5]) { pulled.push(n); yield n; }
}
const [firstPull, secondPull] = counted();
console.log('two names :', first, second);
console.log('skipped slot :', middle);
console.log('spare name :', three, '<- no error, just undefined');
console.log('from a string :', letterA, letterB);
console.log('from a Set :', smallest, next);
console.log('from array-like:', fromArrayLike);
console.log('generator pulls:', pulled, '-> got', firstPull, secondPull);
Output
8 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 8 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('positions map left to right', [first, second], ['rice', 'beans']);
test('a bare comma skips a position', middle, 'beans');
test('a name with nothing to match is undefined', three === undefined, true);
test('the earlier names still fill normally', [one, two], ['rice', 'beans']);
test('a string destructures into characters', [letterA, letterB], ['h', 'i']);
test('a Set destructures in insertion order', [smallest, next], [10, 20]);
test('an array-like object is not iterable, so the pattern throws', fromArrayLike, 'TypeError');
test('only as many values are pulled as the pattern asks for', pulled, [1, 2]);
The last two assertions are the same fact from two sides. An array pattern does not read x[0] and x[1]; it asks for an iterator and takes values off it. That is why { 0: 'zero', length: 1 } throws while a Set, a string or a generator works, and why only two of five values were ever produced.
So destructuring is safe in front of an infinite generator, and it is the wrong tool for a DOM NodeList in an old browser or any other array-like without Symbol.iterator. For those, Array.from first.