What is the best practice in javascript when a function is supposed to return true or false? Can I return falsy values directly instead of true or false? I found this code:
function supportsAPL(handlerInput) {
const supportedInterfaces = handlerInput.requestEnvelope.context.System.device.supportedInterfaces;
const aplInterface = supportedInterfaces['Alexa.Presentation.APL'];
return aplInterface != null && aplInterface !== undefined;
}
and simplified it to this code:
function supportsAPL(handlerInput) {
const {supportedInterfaces} = handlerInput.requestEnvelope.context.System.device;
return supportedInterfaces['Alexa.Presentation.APL'];
}
which works but I'm not sure this is proper/nice javascript. I'm looking for what an experienced javascript developer would write after finding the 1st code snippet (also looking to save lines of code).
return !!supportedInterfaces['Alexa.Presentation.APL']trueorfalse. If the function documents that it returns a any truthy or falsey value, then it can do that and the caller should act accordingly. If I were designing an API, whose main job was to deliver a Boolean result, I'd make sure it returned an actual Boolean (trueorfalse) - just less chance for anyone to ever get confused at minimal cost to make that happen.