jquery – best way in javascript to make console.log calls not cause problems when there’s no console? – Education Career Blog

I’m using firebug and making lots of console.log, .info, .dir calls, etc. When the app runs on a machine with firebug turned off, it can cause errors. What’s the best technique to avoid that? 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’ve also got jQuery on the project if that helps.)


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

  window.console = { log: $.noop, group: $.noop, groupEnd: $.noop };

But whatever functions you’re using, add them in.

Leave a Comment