Last Updated: February 25, 2016
· 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(){}; = 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.error in production environments.

Say Thanks

1 Response
Add your response

Screen shot 2016 02 29 at 20.33.18

just run it before production branches merge

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

Bc0a6794 05ee 11e8 902a 778405599bdb
Software Engineer
Denver, San Francisco
Full Time