console.log(message); is your best friend

If you are into writing a lot of Javascript like me, I’m sure you are frustrated with the lack of good debuggers in the Javascript world. Javascript alert(); statements were the best and the most annoying way to figure out what’s happening in most cases. But recently I discovered the wonders of the console.log. According to my tests, console.log is supported on every browser except IE (no surprise there). Safari, Opera and Chrome supports this out of the box. You will need to install Firebug in Firefox. Do check out Faux console too, it provides a nice workaround for IE.

And if you are a forgetful developer like me, most likely you will leave console.log in your code, causing lot of pain to IE users. IE will throw a javascript error on the page. So as a work around you can use…

MyNameSpace.log = function(message) {
if (typeof (console) != "undefined" && typeof (console.log) != "undefined") {
console.log(message);
}
}

So instead of calling console.log, you can now safely call MyNameSpace.log and not worry about browser incompatibilities.


About this entry