Bad idea to leave "console.log()" calls in your production JavaScript code?

Charlie Kotter picture Charlie Kotter · Jul 11, 2009 · Viewed 20.8k times · Source

I have a bunch of console.log() calls in my JavaScript.

Should I comment them out before I deploy to production?

I'd like to just leave them there, so I don't have to go to the trouble of re-adding the comments later on if I need to do any more debugging. Is this a bad idea?

Answer

Jason Creighton picture Jason Creighton · Jul 11, 2009

It will cause Javascript errors, terminating the execution of the block of Javascript containing the error.

You could, however, define a dummy function that's a no-op when Firebug is not active:

if(typeof console === "undefined") {
    console = { log: function() { } };
}

If you use any methods other than log, you would need to stub out those as well.