I know two methods for setting a default parameter, but I would like to know which method is preferred.
function Foo(par1, par2)
{
if(par2 == null)
par2 = "my default"
}
or
function Foo(par1, par2)
{
par2 = par2 || "my default"
}
or is there a better way than any of these?
EDIT:
I would also like to know how others handle several optional parameters, such as: We have several functions like this in internal libraries (I think they are pretty ugly).
function Foo(par1, par2, par3)
{
if(par2 == null)
par2 = "my default"
if(par3 == null)
par3 = "my default"
}
And to call him:
Foo("Parameter one",null,true)
source
share