Emmanuel Akinfulubi

JavaScriptFundamental (ES6), part 1

Default parameters are evaluated at call time in JavaScript

31 Default parameters run at call time

fair

Give a parameter a default that calls a function, and prove the call happens per invocation rather than once at definition. Then compare = [] with a default that names an outer array, and confirm which arguments trigger a default at all.

Stuck? Three hints, smallest first
  1. 1. The default expression is evaluated on each call that needs it, in the function’s own scope.
  2. 2. So = [] is a fresh array every time — unlike Python, where a mutable default is shared.
  3. 3. undefined triggers a default; null, 0 and '' do not.

The answer

let issued = 0;
const nextId = () => ++issued;
const ticket = (label, id = nextId()) => `${label}#${id}`;

const first = ticket('rice');
const second = ticket('beans');
const supplied = ticket('yam', 99);

const collect = (item, into = []) => { into.push(item); return into; };
const freshA = collect('a');
const freshB = collect('b');

const shared = [];
const collectShared = (item, into = shared) => { into.push(item); return into.length; };
const sharedA = collectShared('a');
const sharedB = collectShared('b');

const greet = (name = 'guest') => name;

console.log('per call     :', first, second);
console.log('value passed :', supplied, '| ids issued:', issued);
console.log('fresh array  :', freshA, freshB);
console.log('shared array :', sharedA, sharedB, shared);
console.log('triggers     :', greet(), greet(undefined), greet(null), JSON.stringify(greet('')));

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('the default expression runs on the call that needs it', first, 'rice#1');
test('and runs again, freshly, on the next one', second, 'beans#2');
test('passing a value skips it, so no id is burnt', [supplied, issued], ['yam#99', 2]);
test('= [] gives every call its own array', [freshA, freshB], [['a'], ['b']]);
test('a default naming an outer array shares that one array', [sharedA, sharedB], [1, 2]);
test('an omitted argument takes the default', greet(), 'guest');
test('so does an explicit undefined', greet(undefined), 'guest');
test('null does not', greet(null), null);
test('nor does an empty string', greet(''), '');

A default is an expression evaluated at call time, not a value captured at definition time. That is why (item, into = []) is safe here and the same shape is a notorious bug in Python: JavaScript builds a new array on every call that omits the argument. The collectShared pair shows what the Python behaviour would look like if you asked for it explicitly.

Because it is an ordinary expression, a default can call a function, read a closure, or throw — const required = () => { throw new Error('missing'); } used as (id = required()) turns an omitted argument into an immediate, clear failure.

Newsletter

New writing, when it lands.

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