Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to use optional arguments in node.js
// example function where arguments 2 and 3 are optional
function example( err, optionalA, optionalB, callback ) {
// retrieve arguments as array
var args = [];
for (var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
// first argument is the error object
// shift() removes the first item from the
// array and returns it
err = args.shift();
// last argument is the callback function.
// pop() removes the last item in the array
// and returns it
callback = args.pop();
// if args still holds items, these are
// your optional items which you could
// retrieve one by one like this:
if (args.length > 0) optionalA = args.shift(); else optionalA = null;
if (args.length > 0) optionalB = args.shift(); else optionalB = null;
// continue as usual: check for errors
if (err) return callback(err);
// for tutorial purposes, log the optional parameters
console.log('optionalA:', optionalA);
console.log('optionalB:', optionalB);
/* do your thing */
} // example()
// invoke example function with and without optional arguments
example(null, function (err) { /* do something */ });
example(null, 'AA', function (err) {});
example(null, 'AAAA', 'BBBB', function (err) {});
@jferrero

This comment has been minimized.

Copy link

@jferrero jferrero commented Dec 4, 2013

mercy

@dandv

This comment has been minimized.

Copy link

@dandv dandv commented Apr 17, 2014

Can we optimize the for loop by replacing it with Array.prototype.push.apply(args, arguments); ?

@rohanorton

This comment has been minimized.

Copy link

@rohanorton rohanorton commented Apr 18, 2014

I had to remove undefined elements from the array as I kept on getting undefined is not a function error when trying to use the callback. Inserted this at line 9 and it seems to work a charm.

args = args.filter(function (val) {
  return val !== undefined;
});
@rohanorton

This comment has been minimized.

Copy link

@rohanorton rohanorton commented Apr 18, 2014

@dandv Array.prototype.push.apply(args, arguments); works nicely for me

@nuxibyte

This comment has been minimized.

Copy link

@nuxibyte nuxibyte commented May 23, 2014

nice. thanks.

@rossipedia

This comment has been minimized.

Copy link

@rossipedia rossipedia commented Jun 14, 2014

@dandv why not just var args = [].slice.call(arguments); ?

@adrianblynch

This comment has been minimized.

Copy link

@adrianblynch adrianblynch commented Oct 24, 2014

How do you know if optionalA or optionalB is intended?

If your pattern is function(required, optional, required) or function(required, optional, optional) I think it's OK to deal with those the way you have or any other way, but if you have a funcation signature like function(required, optional_1, optional_2, required) you have no way of telling what the user meant to send, function(required, optional_1, required) or function(required, optional_2, required)

Or have I missed something?

@mduleone

This comment has been minimized.

Copy link

@mduleone mduleone commented Nov 6, 2014

@adrianblynch That's only the case if optional_1 and optional_2 are independent. If you structure your inputs to function in such a way where optional_2 can be present only if optional_1 is present (even if that requires sending function(required, null, optional_2, required), you don't have to worry about that.

If the signature is function(required, optional_1, optional_2) where optional_1 and optional_2 are independent of each other and you pass in function(required, optional_2), unless you design your input to match specific unique criterion, you're right: there's no way to for the function know that you mean optional_2 instead of optional_1.

Long Story Short: design your function to require optional_1 in order to accept optional_2.

@thinklinux

This comment has been minimized.

Copy link

@thinklinux thinklinux commented Nov 27, 2015

Cool. Thanks!

@grushler

This comment has been minimized.

Copy link

@grushler grushler commented Jan 22, 2016

I get Error: SyntaxError: Unexpected string where the arguments is used. If i remove it, everything seems to work fine. Any help please? Thanks

@mexxik

This comment has been minimized.

Copy link

@mexxik mexxik commented Feb 10, 2016

thanks

@gugonzar

This comment has been minimized.

Copy link

@gugonzar gugonzar commented Jun 3, 2020

Thank you @klovadis and @rossipedia !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.