Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How do default parameters work in JavaScript functions?

Quick answer

A JavaScript default parameter is used when its argument is omitted or explicitly passed as undefined. Passing null does not use the default: the parameter receives null. Other supplied values are used as given, including values such as 0 or an empty string. For example, in function multiply(a, b = 1), multiply(5) and multiply(5, undefined) both return 5, while multiply(5, null) returns 0. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I’ve seen functions written with something like b = 1 in the parameter list. What does that default do, and when is it used?

Geez

It gives the parameter a fallback value. JavaScript uses that value if the corresponding argument is omitted or if the caller explicitly passes undefined. For example, function multiply(a, b = 1) { return a * b; } uses 1 for b in either of those cases. [S1]

The default belongs to the parameter, so you do not need to add a separate check inside the function just to handle a missing second argument. If the caller supplies a value for b, JavaScript uses that supplied value instead of the default. [S1]

Follow-up question

Can you show the difference between leaving the argument out and passing undefined or null?

Geez

Using the same multiplication function, here are the calls and results:

function multiply(a, b = 1) {
  return a * b;
}

multiply(5);            // 5: b is omitted, so b defaults to 1
multiply(5, undefined); // 5: undefined also triggers the default
multiply(5, null);      // 0: b is null, not the default 1
multiply(5, 3);         // 15: b is the supplied value 3

In this multiplication, null is converted to 0, which is why that call returns 0. The key distinction for the default is that undefined triggers it but null does not. [S1]

Follow-up question

Does that mean any value that seems empty or false will use the default?

Geez

No. A default parameter is not a general test for whether a value is empty, false, or missing in a broader sense. For this parameter, explicitly supplied values such as 0, false, or "" are still supplied values; they do not trigger the default just because they may be falsy. [S1]

For example, if you call multiply(5, 0), the second argument is 0, so the result is 0. If your function should treat a particular value such as null as a request to use a fallback, a default parameter alone will not express that rule. Decide that behavior separately and make it clear in the function’s design.

Follow-up question

How should I check a function like this before using it in my own code?

Geez

Write down what should happen for the cases you care about, then call the function with each one. For the multiplication example, the compact check is:

multiply(5);            // 5
multiply(5, undefined); // 5
multiply(5, null);      // 0
multiply(5, 3);         // 15

These results demonstrate the default rule and the effect of the supplied values. They do not establish how a different function will behave for every input: its own operation may handle a supplied value differently. Keep the parameter’s intended fallback visible in the function declaration, and check any special behavior you expect for values such as null.

A practical checklist

  • Identify which parameter should have a fallback value.

  • Choose the default and write it in the parameter list, such as b = 1.

  • Check the behavior when the argument is omitted.

  • Check the behavior when the caller passes undefined and null.

  • Check ordinary values and any falsy values your function is expected to receive.

A few more useful answers

Does passing `undefined` explicitly behave the same as leaving out the argument?

For a parameter with a default, yes: either omission or an explicit undefined uses the default. [S1]

Will `null` make JavaScript use the default parameter?

No. The parameter receives null; the default is not used. What happens next depends on the function’s operation. [S1]

Does passing `0` trigger a default parameter?

No. A supplied 0 is used as the argument rather than replaced with the default. [S1]

Sources

References used for this guide.

  1. [S1] Default parameters — JavaScript | MDN
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work