Forget console.log

I remember the day I learned that I could use console.log to test my app instead of having to alert everything. Man did I feel good and smart. Everything was very smooth, and fast. No longer did I have to close every single alert in order to get to the next one, nor did I had to worry about leaving some rouge alert saying You made it into the if!

But then again every time I tried to test in IE beauty became the beast. Instead of getting a good log in the IE console which… ooops doesnt exist, I got a nasty js error saing what the hell is console.log ?

So I got into the habit of cleaning up my console.logs after using them which really didn’t help me testing out something in staging or dev because when you are making heavy client side apps, everything is ajax based, and you always need to know if you are showing the exact data that you received or if your js code is doing something funky.

Solution simply use another function that checks if console.log exists and if so log it, else don’t


function cLog( text ){
if( (window['console'] !== undefined) ){
console.log( text );
}
}

Updated with a better solution

if(!this.console){
window.console={
log:function(){}
};
}

— Update as of sep 28 2014,

Using a grunt task to remove all your logs helps prevent deployment of logs, and avoid IE9 errors.

https://www.npmjs.org/package/grunt-remove-logging

Enjoy!