Javascript Debugging : console.debug()

posted on January 8, 2009 in Uncategorized

I know! Everyone hates Javascript. Except for this weird breed of us that get high of client side interaction of things. Sadly, when JS was in its infancy most of us were developing in IE6 and error messages for bugs were cryptic and useless . . . and at the time JS was being used for adding falling snow on a site, or a countdown to the year 2000. 

These days there have been many advances in JavaScript, the biggest - in my opinion - is the most ignored of them all : console.debug()

From the old days of JS programming, we would do something like:

var x = 5;
var y = 5;
var z = x+y;
alert(z);

This would pop up an alert box with the number 10 in it.

The problem with using alert()’s is when you try to do a loop and the loop never ends because of a coding error and alert boxes are going off everywhere. At which point you have to shutdown your browser and start all over and *hope* that things would fair better a second time.

There is a cure my friend: console.debug()

To use console.debug() you must be in FireFox with Firebug installed. If you don’t know what Firebug is yet, you have been doing it all wrong for far too long! Write your code as normal, but swap the alert() for a console.debug() :

var x = 5;
var y = 5;
var z = x+y;
alert(z)
console.debug(z)
 

Refresh your page, launch Firebug and there inside is “5″! Amazing isn’t it?! It gets better though! Firebug gives you Object Inspection, which is incredibly useful when working with others libraries or trying to figure out how to access elements in a JSON object:

 

Firebug In Action ( click for bigger image )

Firebug In Action ( click for bigger image )



So there you have it, if you are not using console.debug() - start . . . now! If you are stuck in your alert() ways - I have a solution - override your alert():

window.alert = function(e) {
console.debug(e);
}

Just remember console.debug() does NOT work in IE, nor does it work in Safari, so make sure to comment/remove them before you test in those browsers. If you want to use the Debug Console in Safari ( it is tucked under Develop > Show Error Console ) ( Turn Develop Menu on in Preferences ) you must use :

window.console.log( e );

Also for some reason, you sometimes have to enable Firebug’s console with:

window.loadFirebugConsole();
More information at: http://getfirebug.com/console.html

3 Comments

  1. Do you find this approach easier than inspection with Firebug? I worry that I would forget to remove the statements and cause runtime issues with the non-compatible browsers.

    Comment by Joe — January 10, 2009 @ 9:54 am

  2. I think it depends on the scope of what you are doing, and how many times you rely on console.debug(), I use it much the same way a developer would quickly echo, print, or alert to make sure everything is coming through the way it should - after I am satisfied, I delete it. As far as non-compatible browsers go, you will quickly realize you left a console.debug in at runtime because the script will fail quickly. I have a debug() function around somewhere, that checks whether it can do a console.debug or window.console.log( e ) - if neither, do nothing at all - that allows you to keep all your debug() statements in.

    Also check out Firebug Lite - it works in IE : http://getfirebug.com/lite.html

    Comment by zholmquist — January 10, 2009 @ 10:08 am

  3. Wow. I need to clean and style these comments !

    Comment by zholmquist — January 10, 2009 @ 10:08 am

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.