Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I declare optional function parameters in JavaScript? [duplicate]

Tags:

javascript

Can I declare default parameter like

function myFunc( a, b=0) {   // b is my optional parameter } 

in JavaScript?

like image 552
Uttam Dutta Avatar asked Oct 09 '12 09:10

Uttam Dutta


People also ask

How do you pass optional arguments in JavaScript?

Using the Logical OR operator ('||') In this method, the optional parameter is "Logically ORed" with the default value within the body of the function. In the example below, if the value of b is undefined, 2 is passed instead.

How do you declare an optional parameter in a function?

To declare optional function parameters in JavaScript, there are two approaches: Using the Logical OR operator ('||'): In this approach, the optional parameter is Logically ORed with the default value within the body of the function. Note: The optional parameters should always come at the end on the parameter list.

Can a function have multiple parameters JavaScript?

When you call a function in JavaScript, you can pass in any number of arguments, regardless of what the function declaration specifies. There is no function parameter limit. In the above function, if we pass any number of arguments, the result is always the same because it will take the first two parameters only.


2 Answers

With ES6: This is now part of the language:

function myFunc(a, b = 0) {    // function body } 

Please keep in mind that ES6 checks the values against undefined and not against truthy-ness (so only real undefined values get the default value - falsy values like null will not default).


With ES5:

function myFunc(a,b) {   b = b || 0;    // b will be set either to b or to 0. } 

This works as long as all values you explicitly pass in are truthy. Values that are not truthy as per MiniGod's comment: null, undefined, 0, false, ''

It's pretty common to see JavaScript libraries to do a bunch of checks on optional inputs before the function actually starts.

like image 114
Tigraine Avatar answered Oct 20 '22 00:10

Tigraine


Update

With ES6, this is possible in exactly the manner you have described; a detailed description can be found in the documentation.

Old answer

Default parameters in JavaScript can be implemented in mainly two ways:

function myfunc(a, b) {     // use this if you specifically want to know if b was passed     if (b === undefined) {         // b was not passed     }     // use this if you know that a truthy value comparison will be enough     if (b) {         // b was passed and has truthy value     } else {         // b was not passed or has falsy value     }     // use this to set b to a default value (using truthy comparison)     b = b || "default value"; } 

The expression b || "default value" evaluates the value AND existence of b and returns the value of "default value" if b either doesn't exist or is falsy.

Alternative declaration:

function myfunc(a) {     var b;      // use this to determine whether b was passed or not     if (arguments.length == 1) {         // b was not passed     } else {         b = arguments[1]; // take second argument     } } 

The special "array" arguments is available inside the function; it contains all the arguments, starting from index 0 to N - 1 (where N is the number of arguments passed).

This is typically used to support an unknown number of optional parameters (of the same type); however, stating the expected arguments is preferred!

Further considerations

Although undefined is not writable since ES5, some browsers are known to not enforce this. There are two alternatives you could use if you're worried about this:

b === void 0; typeof b === 'undefined'; // also works for undeclared variables 
like image 32
Ja͢ck Avatar answered Oct 20 '22 00:10

Ja͢ck