Best way in javascript to make console.log calls not cause problems when there is no console?

I use firebug and make many calls to console.log, .info, .dir, etc. When the application starts on a machine with firebug disabled, this can cause errors. What is the best technique to avoid this? This seems to work:

// global scope if (typeof(console) == 'undefined') { console = { info : function() {}, dir : function() {}, error : function() {}, log : function() {} }; } 

but I don't like the idea of ​​manually maintaining a list of console functions. Other ideas?

(We also have jQuery in the project, if that helps.)

+4
source share
1 answer

I personally just use $.noop to shorten it like this:

 if(!window.console) window.console = { log: $.noop, group: $.noop, groupEnd: $.noop }; 

But whatever functions you use, add them.

+3
source

All Articles