Quick testing for console.log

Warning This article was written over six months ago, and may contain outdated information.

I‚Äôm hap¬≠py to see that IE8 includes native sup¬≠port for console.log, the JavaScript com¬≠mand which writes infor¬≠ma¬≠tion to your pre¬≠ferred debug¬≠ging tool (mine is Fire¬≠bug). If you leave it in your code ‚Äď as I did on my lat¬≠est project ‚Äď it throws an error in IE7 & below.

The way around it is to quick­ly check that the com­mand is sup­port­ed by your brows­er, and to pro­vide an alter­na­tive (I use that old stand­by, alert) if not; and the quick­est way to do that is with the if...else short­hand:

window.console ? console.log(foo) : alert(foo);

Comments are closed.