Wednesday, February 16, 2011

Optional Paramter in JavaScript function

I have a function myFun(param1, param2) {
//area1,...;}

When we call this function, we can call:

myFun(1,2);
myFun(1); --param1=1, param2 is undefined
myFun();--param1, param2 undefined

In the area1 place, we can add the sample code like

if(param1===undefined)
param1=0;

if(param2===undefined)
param2=1;

No comments: