Default parameters: when they are evaluated

A default parameter is written as an = followed by an expression in the parameter list. It answers the question the previous lesson left open: what a parameter should hold when the call passes nothing for it. Instead of undefined, the parameter is bound to the value of that expression.

The expression is not a constant and not a snapshot taken when the function is defined. It is code that runs at the moment of a call, in the scope of the function, and only when the argument is undefined. That single rule explains the three sections below: defaults that count their calls, defaults that read the parameters to their left, and defaults that silently do nothing because the caller passed null.

A default fills a missing argument

Both parameters below carry a default. A call with no arguments gets both of them, a call with one gets the second, and a call with two overrides both. The fourth line passes undefined on purpose, which counts as missing, and the fifth passes null, which does not.

function greet(name = 'world', punctuation = '!') { return `Hello, ${name}${punctuation}`; } console.log(greet()); console.log(greet('Ada')); console.log(greet('Ada', '.')); console.log(greet(undefined, '?')); console.log(greet(null));

node greet.js

Hello, world!
Hello, Ada!
Hello, Ada.
Hello, world?
Hello, null!

The last line is the trap. null is a value, so the default does not run and the function receives it, and the template string turns it into the word null. A default guards against a missing argument, not against a caller that means to clear a value, which is why a function that accepts null as "no value" has to say so in its body.

When a default is evaluated

The expression of a default is evaluated on every call that needs it, which makes a counter a direct way to see it. The program below passes no argument twice and a value once, and the counter reports how often the default expression really ran.

let calls = 0; function fresh() { calls += 1; return `value-${calls}`; } function withDefault(value = fresh()) { return value; } console.log(withDefault()); console.log(withDefault()); console.log(withDefault('given')); console.log('fresh called:', calls);

node when.js

value-1
value-2
given
fresh called: 2

Two calls without an argument gave two different strings, so a default is not a value stored once at definition time: it is a short function the engine runs per call. The third call passed its own argument and the counter stayed at two, so the expression was skipped entirely. That is the behaviour a default that builds an array or an object depends on, because a shared default would otherwise be one mutable value for every call of the function.

A default that uses an earlier parameter

The parameters of a function are bound from left to right, so a default may read the ones already bound. The scope around the function is visible too, which is how a default reaches a constant of the module.

function range(start, end = start + 4, step = 1) { const values = []; for (let value = start; value <= end; value += step) { values.push(value); } return values; } console.log(range(1).join(',')); console.log(range(1, 3).join(',')); console.log(range(1, 7, 3).join(',')); const taxRate = 0.2; function withTax(amount, tax = amount * taxRate) { return amount + tax; } console.log('with tax:', withTax(100)); console.log('explicit tax:', withTax(100, 5));

node earlier.js

1,2,3,4,5
1,2,3
1,4,7
with tax: 120
explicit tax: 105

range(1) used the default of end, which read start and produced 5, and the loop stopped there. The second call supplied its own end and the third supplied all three. In withTax the default of tax does arithmetic on the first parameter and on a constant of the module, and the explicit call replaces it with the number given. Defaults are the most common place where a short expression is worth more than a sentence of documentation.

undefined, null and the order of parameters

Only undefined triggers a default. A missing argument, an explicit undefined and a parameter that was never bound all give the same result, while null and the empty string are values and pass through. The parameters are bound from left to right, so a default that reads a parameter to its right meets a binding that is not initialised yet, and the temporal dead zone throws.

function greet(name = 'world') { return `Hello, ${name}`; } console.log('missing:', greet()); console.log('undefined:', greet(undefined)); console.log('null:', greet(null)); console.log('empty:', JSON.stringify(greet(''))); function ordered(first = second, second = 2) { return first + second; } try { ordered(); } catch (error) { console.log(error.name + ': ' + error.message); }

node order.js

missing: Hello, world
undefined: Hello, world
null: Hello, null
empty: "Hello, "
ReferenceError: Cannot access 'second' before initialization

The first two lines print the same sentence, which is why passing undefined on purpose is the way to ask for a default when a value comes from somewhere else. The fourth line shows that an empty string is a value too, so a function that treats it as missing needs a body that says so. The error of the last lines is the same temporal dead zone as in the lesson on block scope: second is not bound yet when the default for first runs, and the fix is to swap the two parameters.

Exercises

  1. Call greet from greet.js with an empty string as the name. Which word stands where the name should be, and why does the default not apply?
  2. Pass 0 as the punctuation in greet.js. What does the sentence end with?
  3. Change the default of withDefault in when.js to fresh without the parentheses. What does each call print, and what does calls report?
  4. Add a fourth call to range in earlier.js that passes the step but not the end. What does it take to skip the middle argument, and which value would the end get?
  5. Swap the two parameters of ordered so that the one with the default comes first. Does the call succeed, and what does it return?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. When is the expression of a default parameter evaluated?

    Answer

    At every call that needs it

    A default is code that runs per call, and only when the argument for that parameter is undefined.

  2. Which value triggers a default parameter?

    Answer

    undefined

    Only undefined, and a missing argument, asks for the default; null and the empty string are values.

  3. What does this program print? function f(a = 1, b = a + 1) { return a + b; } console.log(f());

    Answer

    3

    The first default binds a to 1, and the default of b reads that value and adds one, so the sum is 3.

  4. What does this program print? function f(a = 2) { return a; } console.log(f(null));

    Answer

    null

    null is a value and does not trigger the default, so the parameter holds null and the call returns it.

  5. What does this program print? let n = 0; function next() { n += 1; return n; } function f(a = next()) { return a; } console.log(f(), f(), f(10), n);

    Answer

    1 2 10 2

    The first two calls run the default and the counter reaches 2; the third call passes its own value and skips it.

  6. Which parameter may a default expression read?

    Answer

    An earlier parameter, and the scope around the function

    Parameters are bound from left to right, so the ones to the left are ready and the ones to the right are not.

  7. What does this program print? function f(a = b, b = 1) { return a + b; } try { console.log(f()); } catch (error) { console.log(error.name); }

    Answer

    ReferenceError

    The default of the first parameter reads the second one before it is bound, which is the temporal dead zone.

  8. What does this program print? ``function f(a, b = a) { return ${a}-${b}; } console.log(f(3));``

    Answer

    3-3

    The second parameter takes its default from the first, which the call already bound to 3.

  9. Why is a default of [] or {} safe for a function that changes the value?

    Answer

    Because the expression runs per call, so every call gets a new value

    A default evaluated at each call builds a fresh array or object, so one call cannot see the changes of another.

  10. What happens to a default when the caller passes undefined explicitly?

    Answer

    The default runs, exactly as for a missing argument

    An explicit undefined is indistinguishable from a missing argument, which is why it is a way to ask for the default.

The code on this page was run with Node.js 24.22.1 and linted with ESLint 10.14.0 on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev