csland.com

Independant Computer Consultant

Software Consulting, Development, Strategy, and Programming
Home >  How to pass default parameters to JavaScript function?

How to pass default parameters to JavaScript function?

02/23/2010
7235 hits
      Javascript doesn't have default argument in function definition. However, JavaScript doesn't require to call function with exactly the same argument.
      
      For example, a function defined as followings:
      function apple(a, b, c) { ........ }
      
      You can call this function:
      
      apple(4, 7, 9);
      or
      apple(4, 7);
      or
      apple(4);
      or
      apple();
      even or
      apple(5, 6, 8, 13, 50, 89); At this case, only the first three arguments are passed to the function, the other three will be ignored.
      
      So, inside of the function, we can use these statement to define the default values for a, b and c:
      
      function apple(a, b, c){
       a=(a)?a:"default a value";
       b=(b)?b:"default b value";
       c=(c)?c:"default c value";
      
      .......
      }

[02/24/2010, 05:14]

References

CSLand.Com © 1996-2009. All Rights Reserved