I’ve always handled optional parameters in JavaScript like this:
function myFunc(requiredArg, optionalArg){ optionalArg = optionalArg || 'defaultValue'; // Do stuff }
Is there a better way to do it?
Are there any cases where using || like that is going to fail?
Your logic fails if optionalArg is passed, but evaluates as false – try this as an alternative
Or an alternative idiom:
Use whichever idiom communicates the intent best to you!