What is the best way to create a function that takes optional arguments in nodejs?
for example I know this method:-
function optionalArguments(a,b){
var a = a || "nothing";
var b = b || "nothing";
}
but in this case if I do this :
optionalArguments(false,false)
both a and b return "nothing" although I have passed an argument.
and also I get unexpected token error when I call the function like this :
optionalArguments(,"xxx");
Is there a better or a standard method to tackle optional arguments in nodejs?
Any help is appreciated. Thanks in advance.
Optional arguments enable you to omit arguments for some parameters. Both techniques can be used with methods, indexers, constructors, and delegates. When you use named and optional arguments, the arguments are evaluated in the order in which they appear in the argument list, not the parameter list.
You can assign an optional argument using the assignment operator in a function definition or using the Python **kwargs statement. There are two types of arguments a Python function can accept: positional and optional. Optional arguments are values that do not need to be specified for a function to be called.
So, it is optional during a call. If a value is provided, it will overwrite the default value. Any number of arguments in a function can have a default value.
If you expect a falsy value, you must explicitly check for argument === undefined . This method only allows the last arguments to be optional - you cannot make an optional first parameter, middle parameter, or combination of parameters optional. The next methods let you position optional arguments anywhere.
If you are using Node.js v6 (or higher), you have access to default parameters.
function optionalArguments(a="nothing", b="nothing") {
return `a: ${a}, b: ${b}`;
}
Then
optionalArguments(false, false) // 'a: false, b: false'
optionalArguments('this') // 'a: this, b: nothing'
optionalArguments() // 'a: nothing, b: nothing'
optionalArguments(undefined,'that') // 'a: nothing, b: that'
You do that exactly like for client side javascript.
The way you suggest does work but is, as you noticed, painful when the arguments that can be omitted aren't the last ones.
In that case, what's commonly used is an "options" object :
function optionalArguments(options){
var a = options.a || "nothing";
var b = options.b || "nothing";
}
Note that ||
is dangerous. If you want to be able to set arguments like false
, ""
, 0
, NaN
, null
, you have to do it like this :
function optionalArguments(options){
var a = options.a !== undefined ? options.a : "nothing";
var b = options.b !== undefined ? options.b : "nothing";
}
A utility function can be handy if you do this a lot :
function opt(options, name, default){
return options && options[name]!==undefined ? options[name] : default;
}
function optionalArguments(options){
var a = opt(options, 'a', "nothing");
var b = opt(options, 'b', "nothing");
}
This way you can even call your function with
optionalArguments();
The ||
is just the regular old or operator. It comes in handy when the value is expected to not be falsey. But, if values like 0
, false
, or null
are valid and expected, you need to take a different approach.
== null
To check to see if a non-null value was passed, use == null
. This will return true
when null
or undefined
are passed in:
function optionalArguments (a, b) {
a = a == null ? "nothing" : a;
b = b == null ? "nothing" : b;
...
}
In most cases, this is the best approach for implementing optional parameters. It allows the caller to pass null
when the default value is desired. It's particularly useful when a caller wants to pass a value for the second argument, but use the default for the first. Eg, optionalArguments(null, 22)
=== undefined
If null is a valid and expected value, compare as above using undefined
and the ===
operator. Make sure that you are using a valid value of undefined
for your comparison. It is possible for a script to say var undefined = 0
, causing endless headaches for you. You can always do === void 0
to test for undefined
.
arguments.length
What if I call your function like this?
optionalArguments("something", void 0);
In this case, I did pass a value, but that value is undefined
. There may be times when you truly want to detect whether an argument was passed in or not. In this case, you need to check the arguments.length
:
function optionalArguments (a, b) {
a = arguments.length > 0 ? a : "nothing";
b = arguments.length > 1 ? b : "nothing";
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With