1. Home
  2. Computing & Technology
  3. JavaScript

Optional Function Arguments

Join the Discussion

Questions? Comments?

In most programming languages when you define a function you specify both the number and type of arguments that you expect to be passed to your function. many also allow you to define one or more arguments on the end of the list as being optional by specifying the values that those arguments should use if there are fewer parameters passed to the function. If fewer parameters are passed without default values being defined for the rest then the compiler would give an error. Similarly the compiler would give an error if more parameters were passed than arguments are defined.

Javascript doesn't work that way.

The most obvious differences are that Javascript does not use strong data typing and so arguments don't have a type specified for them but instead take on whatever data type is passed to them. Also Javascript is interpreted and not compiled so any mismatches would be detected at run time rather than compile time since there is no compile step.

There are actually lots more differences than this though. Javascript doesn't allow you to specify arguments as optional in the function declaration itself. Where most other languages allow you to specify the default value for optional arguments and hence which arguments are actually optional within the data declaration, Javascript allows all arguments to be optional but requires that you test if a parameter has been passed and assign the default within the function itself. You do it like this:

function myFunction(arg1, arg2) {
if (typeof arg2 == 'undefined' ) arg2 = 'default';
...

Probably the most unexpected difference is that Javascript allows you to pass more parameters to the function than the number of arguments and provides an alternative way for the function to access all of the arguments passed to the function whether you gave them names in the function declaration or not.

Each function has an associated arguments object. This object acts a little like an array in that all of the arguments passed to the function can be referenced using arguments[0], arguments[1], etc and the total number of arguments can be accessed using arguments.length. That is as far as the similarity to arrays goes though as none of the other methods and properties that can be used with arrays can be used with the arguments object. In fact there is only one other property that the arguments object offers. arguments.callee provides a way to reference the actual code within the function itself.

By making use of the arguments object associated with each function Javascript enables us to set up functions that have a completely variable number of parameters passed to them. This makes Javascript far more flexible than most other languages which are limited to what I described in the first paragraph.

Explore JavaScript
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. JavaScript

©2009 About.com, a part of The New York Times Company.

All rights reserved.