JavaScript style for optional callbacks

I personally prefer

typeof callback === 'function' && callback();

The typeof command is dodgy however and should only be used for "undefined" and "function"

The problems with the typeof !== undefined is that the user might pass in a value that is defined and not a function

Leave a Comment