I was wondering, can you create a function with an optional parameter.
Example:
function parameterTest(test)
{
if exists(test)
{
alert('the parameter exists...');
}
else
{
alert('The parameter doesn\'t exist...');
}
}
So if you call parameterTest() then the result would be a message “The parameter doesn’t exist…”. And if you call parameterTest(true) then it would return “the parameter exists…”.
Is this possible?
This is a very frequent pattern.
You can test it using
You can then call your function with one of those forms :
Be careful not to make the frequent error of testing
Because you wouldn’t be able to differentiate an unprovided value from
false,0or"".