Last Updated: February 25, 2016
·
1.276K
· ipetepete

Excuse me, your console.log is breaking things

Its not a good idea to leave your debugging statements in your code. But it sometimes makes sense to have state/informational statements for quick verification on a production environment.

And sometimes we just plain forget to get rid of those pesky console.log statements.

Make a fallback to prevent JS errors when the console is not defined:

if( typeof console !== "undefined"){
    console.log("The console is working");
} else {
    console = {};
    console.log = function(){};
    console.dir = function(){};
    console.info = function(){};
    console.warn = function(){};
    console.err = function(){};
    console.error = function(){};
}

This way, if console is not available, it won't break scripts (mostly an issue in IE).

Or even better, adapt it to only use console.info, console.error in production environments.

1 Response
Add your response

just run it before production branches merge

$ ack 'console.log' -r
over 1 year ago ·