I have a piece of JavaScript code as follows:
function main(condition){
if(condition){
doSomething();
return obj;
}
}
now I want to refactor this code to get rid of the “if” statement. Here is what I want to do
function main(condition){
var doSomethingAndReturnObj = function(){
doSomething();
return obj;
}
return condition && doSomethingAndReturnObj();
}
here is where I need help. The caller of main function expects a return value of “undefined” or an obj. In my refactored code, would my
return condition && doSomethingAndReturnObj();
convert the return value to a true and false type?
Thanks for your replies.
JavaScript’s
&&operator is coalescing.