Emmanuel Akinfulubi

JavaScriptFundamental (ES6), part 1

Default values in array destructuring in JavaScript

18 Defaults in an array pattern

fair

Give array pattern positions defaults, then work out exactly which incoming values trigger them: a missing slot, an explicit undefined, null, 0. Also check whether a default expression runs when it is not needed.

Stuck? Three hints, smallest first
  1. 1. A default fires when the matched value is undefined — and only then.
  2. 2. null is a value, so it is kept. So are 0, '' and false.
  3. 3. Defaults are evaluated lazily and left to right, so a later one can refer to an earlier binding.

The answer

let builds = 0;
const build = () => { builds++; return 'built'; };

const [missing = 'fallback'] = [];
const [explicit = 'fallback'] = [undefined];
const [nulled = 'fallback'] = [null];
const [zero = 'fallback'] = [0];
const [blank = 'fallback'] = [''];

const [width = 2, height = width * 3] = [];       // a later default reads an earlier name
const [, secondOnly = 'filled'] = ['first'];      // the skipped slot is unaffected

const [supplied = build()] = ['given'];           // build() must not run
const [absent = build()] = [];                    // this one must

console.log('missing slot   :', missing);
console.log('explicit undef :', explicit);
console.log('null           :', nulled, '<- kept');
console.log('zero and blank :', zero, JSON.stringify(blank), '<- also kept');
console.log('chained default:', width, height);
console.log('builds counted :', builds, '-> supplied:', supplied, 'absent:', absent);

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('a default fills a position that has no value', missing, 'fallback');
test('an explicit undefined fires it too', explicit, 'fallback');
test('null does not, because null is a value', nulled, null);
test('nor does zero', zero, 0);
test('nor an empty string', blank, '');
test('a later default may read an earlier binding', [width, height], [2, 6]);
test('a skipped position takes its own default', secondOnly, 'filled');
test('a default expression is skipped when a value arrived', supplied, 'given');
test('so it ran exactly once, for the position that needed it', builds, 1);

undefined triggers a default; null does not. That single rule is behind a whole class of bug: an API that returns null for "no value" defeats every default you write, and opts.retries = 0 is a real setting that || would throw away. Defaults respect 0, '' and false; || does not. Exercise 40 has the operator for the null case.

Laziness is worth knowing when the default is expensive or has a side effect. const [id = nextId()] = args does not burn an id when one was passed.

Newsletter

New writing, when it lands.

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