4

ECMAScript 6 introduced default parameters. The default value can be any expression, so you can use it to make a function throw an error when a parameter is not supplied:

const mandatory = (name) => {
  throw new Error(`The ${name} parameter is mandatory`);
};

const foo = (bar = mandatory('bar')) => {
  // some code
};

foo(42); // Works fine.
foo(); // Error: The bar parameter is mandatory

What are the advantages and disadvantages of this? Is it a good practice, or an abuse of default parameters?

2
  • This looks fine to me. I do not think it is "functionally" any different than checking against undefined in the function itself. All I would recommend is that you keep it consistent across your code base. Good conventions will make your code easier to maintain. (Also, verify that the browsers you support have implemented this functionality.)
    – Caleb
    Commented Dec 25, 2016 at 17:41
  • @Caleb I'm not asking whether it is functionally different, but whether it is (more) readable. Commented Dec 25, 2016 at 18:31

1 Answer 1

5

I think you are abusing the functionality a little bit.

The point of default parameters is providing a value which make sense in case calls to this function don't pass some definite value for that parameter.

There is a big overlap between not passing a definite value and forgetting a mandatory parameter, but there is a difference: you can pass undefined.

Your code will throw if the function is called with undefined:

const mandatory = (name) => {
  throw new Error(`The ${name} parameter is mandatory`);
};
const foo = (bar = mandatory('bar')) => {
  // some code
};
foo(undefined); // Error: The bar parameter is mandatory

In this case complaining about the parameter being mandatory is misleading, because a parameter has been passed indeed.

undefined might not be an allowed value, but in this case you should check the value and complain about its type.

If you only want to check if an argument has been passed or not, using a normal function and checking arguments.length may be a better idea.

See for example the behavior of DOMParser#parseFromString, which in my opinion is more helpful:

var p = new DOMParser();
p.parseFromString();                     // TypeError: Not enough arguments
p.parseFromString(undefined);            // TypeError: Not enough arguments
p.parseFromString(undefined, undefined); // TypeError: Argument 2 'undefined' is not a valid value
2
  • 1
    How is passing undefined as an argument different from calling a function without arguments? The standard way to check if an arguments has been passed or not is to compare the parameter to undefined, which works exactly as using default parameters. Commented Dec 25, 2016 at 20:03
  • 4
    @Gothdo Passing undefined may behave differently than passing no argument, that's the point. For example, compare Array() and Array(undefined).
    – Oriol
    Commented Dec 25, 2016 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.