Emmanuel Akinfulubi

JavaScriptFundamental (ES6), part 1

Rest element in array destructuring in JavaScript

19 A rest element in an array pattern

fair

Split a list into a head and a tail with ..., confirm the tail is a genuinely new array, and find the three things the parser will not let a rest element do.

Stuck? Three hints, smallest first
  1. 1. const [head, ...tail] = xs. The tail is a real array, empty if nothing is left.
  2. 2. A rest element must be the last thing in the pattern — no default, no trailing comma.
  3. 3. The rest position may itself hold a pattern: [a, ...[b, c]].

The answer

const parses = (src) => {
  try { new Function(src); return 'parsed'; } catch (e) { return e.constructor.name; }
};

const [head, ...tail] = [1, 2, 3];
const [only, ...none] = ['solo'];
const [...letters] = 'Yaba';

const source = [1, 2, 3];
const [, ...copiedTail] = source;
copiedTail.push(4);                             // the source must not notice

const [firstOfSet, ...restOfSet] = new Set(['a', 'b', 'c']);
const [p, ...[q, r]] = [1, 2, 3];               // a pattern in the rest position

const notLast = parses('const [...r, last] = [1, 2];');
const withDefault = parses('const [...r = []] = [];');
const trailingComma = parses('const [a, ...b,] = [1, 2];');

console.log('head and tail :', head, tail);
console.log('nothing left  :', none, 'length', none.length);
console.log('from a string :', letters);
console.log('source intact :', source, 'tail now', copiedTail);
console.log('nested rest   :', p, q, r);
console.log('rest not last :', notLast, '| with a default:', withDefault, '| trailing comma:', trailingComma);

Output


      
9 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 9 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

9 checks

test('rest collects everything after the named positions', tail, [2, 3]);
test('and is an empty array when there is nothing left', none, []);
test('a string spreads into its characters', letters, ['Y', 'a', 'b', 'a']);
test('the rest array is new, so pushing to it leaves the source alone', source.length, 3);
test('any iterable works, so a Set splits the same way', [firstOfSet, restOfSet], ['a', ['b', 'c']]);
test('the rest position may hold another pattern', [p, q, r], [1, 2, 3]);
test('a rest element must come last', notLast, 'SyntaxError');
test('it may not carry a default', withDefault, 'SyntaxError');
test('and it may not be followed by a comma', trailingComma, 'SyntaxError');

The rest element is where destructuring earns its keep on a list of unknown length: const [command, ...args] = input in one line, with args already an array rather than an arguments object or a slice(1) call.

The three refusals all follow from one rule: rest means "the remainder", so nothing can come after it and it can never be absent — the worst case is [], which is why a default would be meaningless. The trailing comma is the one that catches people, because every other list in JavaScript tolerates one.

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.