Optional Parameter in JavaScript Function

Yes you heard it right, C# brought optional parameter in version 4.0 however it is part of JavaScript from beginning of the language design.

JavaScript function,

  1. Does not do any type checking on argument values
  2. Does not do any checking on number of arguments passed

There are three ways you can call a function in JavaScript

  1. With exact number of arguments
  2. With more number of arguments than specified
  3. With less number of arguments than specified

 

With less numbers of arguments

When you call a function with less numbers of arguments then left arguments are passed as undefined. To understand it more let us consider below example,

image

 

When creating function you may want to check for arguments which being not passed and assigned a default value. There are two ways you can assigned a default vale

  1. Using If statement
  2. Using || operator

Let us assume that you want to assign default value for optional parameter as 99. You can do that as following

image

 

Or better way to assign default value is using or operator. If first argument is falsy object in OR operator then it will always use second object. So when you don’t pass optional argument its values is undefined (falsy) and default value will get assigned.

image

 

Always remember to put optional arguments at end of argument list. You cannot make first argument as optional argument and pass second argument. If you want to do this then you will have to explicitly pass undefined as first parameter. So you can make first argument optional as below,

 

image

 

With more numbers of arguments

Other possibility is that you are calling a function with more number of arguments. So for example you are calling above function as following,

 

image

There is no direct way extra arguments can be read inside function. But there is a way using that extra arguments can be read. We can read extra arguments using arguments object. So you can read extra arguments as following

image

 

You can check exact number of arguments as following and throw exception

image

So this is all what I have in this blog to share with you on Optional arguments in JavaScript function. I hope you find this post useful. Thanks for reading.

5 thoughts on “Optional Parameter in JavaScript Function

  1. Perfect explanation DJ. Just observed that in 2nd example- assign default value for optional parameter, an extra ‘=’ is placed. I guess it should be

    if(c==undefined) in place of
    if(c === undefined)

    Please correct me if I am wrong.
    Thanks

  2. Pingback: Dew Drop – July 3, 2013 (#1,578) | Alvin Ashcraft's Morning Dew

  3. Pingback: Optional Parameter in JavaScript Function | debug mode…… | My Readings/Likes

  4. Pingback: Dew Drop – July 5, 2013 (#1,579) | Alvin Ashcraft's Morning Dew

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s