I want to write a function that takes an optional argument and changes its behavior depending on whether or not that argument is provided. This is the first way I thought to do this:
function foo(optionalArg) {
if (optionalArg) {
// do something with the optionalArg
} else {
// do something different
}
}
Is this the best way to do this, or is there a safer/better way?
EDIT: If you actually read the question, you'll notice that it's different than the "duplicates" some claim this is. I know how to provide default values to a function's arguments; I found those links when I was trying to figure this out on my own first. But I'm not looking for how to do that. I'm looking for the best way to change my function's behavior based on whether or not the argument was provided.