Why does the following line sometimes yield the message “undefined is not a function” in the chrome dev console:
(callbackOrUndefined || function() {})();
The idea is to execute callback if it is truthy, i.e. a function, otherwise execute the empty function.
I had to replace it with :
if (callbackOrUndefined !== undefined) callbackOrUndefined();
Edit: I guess I wasn’t clear enough. It sometimes seem to evaluate the block to undefined(); and I don’t understand how and why.
That’s because
callbackOrUndefinedhas a falsy value and then the null function is selected, and when called, it returnsundefined.Notice that you may get
undefinedeven ifcallbackOrUndefineddoesn’t return a defined value…