JS: default argument parameter values

In some languages, you can set default values โ€‹โ€‹for function arguments:

function Foo(arg1 = 50, arg2 = 'default') { //... } 

How do you do this in JavaScript?

+7
source share
4 answers

In JavaScript, nothing that is set is set to undefined . This means that if you want to set default values โ€‹โ€‹for a function, your first line should be a check to determine if these values โ€‹โ€‹are defined:

 function Foo(arg1, arg2) { if (typeof(arg1) === "undefined") { arg1 = 50; } if (typeof(arg2) === "undefined") { arg2 = "default"; } } 

You can shorten the time a little if you know exactly what these values โ€‹โ€‹are:

 function Foo(arg1, arg2) { arg1 = arg1 || 50; arg2 = arg2 || "default"; } 

However, if these arguments are false, they will be replaced. This means that if arg1 is an empty string, null , undefined , false or 0 , it will be changed to 50, so be careful if you decide to use it

+15
source

I prefer checking == null instead of typeof(arg1) === undefined (as the jQuery library does internally: http://docs.jquery.com/JQuery_Core_Style_Guidelines#JSLint ). It is not only shorter, but also handles both null and undefined, so client code can pass in null and get by default.

It is also possible to wrap a function in another function that supplies default values. The prototypejs library has a function that provides argumentNames() (or if you don't want to use a prototype, you can just borrow the implementation - it's just a regular expression that applies to the results of the toString() function).

So you can do something like this:

 var Foo = defaults(Foo, { arg1: 50, arg2: 'default' }); function Foo(arg1, arg2) { //... } 

And the defaults() implementation will look something like this:

 function defaults(fn, def) { return function() { var arg_names = fn.argumentNames(); var nArgs = args.length; for (var nArg = 0; nArg < nArgs; ++nArg) { if (arguments[nArg] == null && arg_names[nArg] in def) arguments[nArg] = def[arg_names[nArg]]; } fn.apply(this, arguments); } } 

Of course, this unverified code and how to write it requires the Prototype library. But all that said, the == null approach inside the function is more natural for Javascript, itโ€™s easier to debug (you donโ€™t have another functional level for the transition), and itโ€™s easier for me to read:

 function Foo(arg1, arg2) { if (arg1 == null) arg1 = 50; if (arg2 == null) arg2 = 'default'; } 
+5
source

You would need to do this in the body of the function:

 function Foo(arg1 ,arg2) { if( typeof arg1 === 'undefined' ) arg1 = 50; if( typeof arg2 === 'undefined' ) arg2 = 'default'; //... } 

Another way to check them for undefined or null as follows:

 function Foo(arg1 ,arg2) { if( arg1 == undefined ) arg1 = 50; if( arg2 == undefined ) arg2 = 'default'; //... } 

Some people do not like this because you can override the value undefined , since this is just a global property. Until this is overridden, it will work fine. If the parameters are null or undefined , they will be set by default.

+1
source

As in ES2015 (ES6), the default parameters can be set as follows:

 function multiply (a, b = 2) { return a * b; } multiply(5); // 10 
+1
source

All Articles