Tip Details Add a Related Tip

Rate as: Positive Negative

Using optional parameters in Javascript functions

Most programming languages have a mechanism which allows optional parameters to be passed to a function/method and if they are not included in the call, default values are set. This is not the case in Javascript. But thankfully, it is still possible to set up a function that take optional parameters.

Consider a function defined like so:

function myFuncName(param1, param2) {

In Javascript, it is perfectly legal to make a call to this function in any of three ways:

myFuncName(1, 2);
myFuncName(1);
myFuncName();

They will all work. The rub in javascript is that if you want the parameter to be optional, you need to add code in the function itself to deal with the cases when a parameter value is not given in the call. For example, consider the function above where we want to make the second parameter optional and if the second parameter is not given, we set a default value of '3.14'. That can be done like so:

function myFuncName(param1, param2) {
   // Set the optional parameter if needed
   if ( param2 === undefined ) {
      param2 = '3.14';
   }

   ...
}

You can do this for as many parameters as you like, but you need to be sure to order the parameters so that the "most optional" ones are further to the right since as soon as you leave off one parameter, it starts removing values from the right-hand side of the parameter list.
Rating: 100% positive, 17 total Votes
Categories: javascript programming
Added: on Apr 19, 2007 at 11:43 am
Added By: an anonymous user

Comments on this Tip

Add a Comment
brilliant!
– Added by an anonymous user on Apr 09, 2008 at 8:25 am
Great stuff - thank you! DC
– Added by an anonymous user on Apr 14, 2008 at 7:53 am
Very nice tip!
– Added by an anonymous user on May 05, 2008 at 6:41 am
Your Comment:
(how to format)
Rate This Tip:

Verify Humanity:
Sorry, we know it's annoying, but please enter the characters shown in the image to the left so that we know you're an actual person and not an evil spammer. Thanks.
       

Related Tips

Static variables in Javascript

100% positive, 3 comments - last added on Aug 15, 2008 at 10:42 am
– Tip added by an anonymous user on Aug 03, 2007 at 4:19 pm

Scrolling HTML elements using the keyboard

100% positive, 1 comment - last added on Jul 18, 2008 at 3:53 pm
– Tip added by rlansky on Jun 22, 2007 at 9:34 am

Using ASP.Net variables in your Javascript code

100% positive, 2 comments - last added on Sep 04, 2007 at 7:12 am
– Tip added by an anonymous user on May 05, 2007 at 7:00 am

Telling when an iframe is done loading

100% positive, 0 comments
– Tip added by an anonymous user on Apr 18, 2008 at 8:05 am

Trimming whitespace in Javascript

Categories: javascript programming
100% positive, 0 comments
– Tip added by an anonymous user on Sep 13, 2007 at 1:54 pm

Great AJAX repository

100% positive, 0 comments
– Tip added by hindy on May 02, 2007 at 7:23 am

Matching any character in a javascript regular expression

100% positive, 0 comments
– Tip added by an anonymous user on Aug 18, 2008 at 9:06 am

Optional parameters in javascript functions

100% positive, 0 comments
– Tip added by an anonymous user on Jul 30, 2008 at 10:44 am

Detecting if a variable exists in Javascript

100% positive, 0 comments
– Tip added by an anonymous user on Jul 22, 2008 at 8:31 am

Using optional parameter in javascript

Categories: javascript programming
100% positive, 0 comments
– Tip added by an anonymous user on Jul 17, 2008 at 7:49 am

Javascript Error: unterminated regular expression literal

100% positive, 0 comments
– Tip added by marty on Jun 26, 2008 at 1:20 pm

compressing javascript

100% positive, 0 comments
– Tip added by an anonymous user on Jun 12, 2008 at 3:02 pm

Javascript variable naming

100% positive, 1 comment - last added on Jul 22, 2008 at 8:33 am
– Tip added by an anonymous user on Sep 30, 2007 at 7:28 am

Creating vector graphics in javascript

100% positive, 0 comments
– Tip added by Marcos84 on May 10, 2007 at 2:44 pm

attaching events to nodes in Javascript

no ratings, 0 comments
– Tip added by an anonymous user on Aug 18, 2008 at 11:23 am

animating elements with jQuery

no ratings, 0 comments
– Tip added by an anonymous user on Aug 04, 2008 at 1:46 pm

Javascript: How to Create a Random Number

0% positive, 0 comments
– Tip added by Walkere on Feb 23, 2008 at 9:49 pm

Code for creating Javascript CSS charts

no ratings, 0 comments
– Tip added by an anonymous user on Aug 21, 2007 at 12:23 pm

Efficient building of large strings in Javascript

no ratings, 0 comments
– Tip added by an anonymous user on Aug 08, 2007 at 12:52 pm

Avoid invalid title names when opening javascript windows

no ratings, 0 comments
– Tip added by an anonymous user on Aug 07, 2007 at 9:36 am