FireBug's Console For Debugging Javascript Data (Thanks Ray Camden)

Posted January 14, 2009 at 8:52 AM by Ben Nadel

Tags: Javascript / DHTML

I love FireBug. I think it is the most amazing FireFox plugin, hands down. I don't think I go through more than an hour of development work without using it; it makes debugging CSS and XHTML issues mere child's play. The way it breaks down CSS rules and shows you the cascading nature of the various rules as they are applied to a selected DOM element and which CSS files each rule came from... you can't even choose the right words to fully express the usefulness of that.

This morning, thanks to Ray Camden, FireBug just got exponentially cooler! He introduced me to FireBug's "console" Javascript object. Of course, I've heard of console.log() before in passing, but I just never looked into it. To be honest, FireBug is such a huge plugin that once I started to use the CSS, XHTML Inspection, and NET activity features, I simply stopped looking into its other features; I figured that I had already squeezed all the tasty goodness out of it.

But that has all changed! The Console object has two methods that I think will revolutionize the way I debug my Javascript. Those methods are log() and dir(). Log() prints a message to FireBug's console. Dir() prints an interactive object explorer to the console. Let's take a quick look at a small example:

  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  • <html>
  • <head>
  • <title>FireBug Console Testing</title>
  •  
  • <script type="text/javascript">
  •  
  • // Define complex object.
  • var objGirl = {
  • Name: "Molly",
  • Hair: "Brunette",
  • Eyes: "Brown",
  • BestQualities:
  • [
  • "Smile",
  • "Laugh"
  • ]
  • };
  •  
  • // Debug data in FireBug.
  • console.log( objGirl );
  • console.dir( objGirl );
  •  
  • </script>
  • </head>
  • <body>
  •  
  • <h1>
  • FireBug Console Testing
  • </h1>
  •  
  • </body>
  • </html>

As you can see in this example, we are building a complex Javascript object. We then pass it to both the log() and dir() methods. This results in the following FireBug console output:

 
 
 
 
 
 
FireBug's Console Output For Complex Javascript Objects. 
 
 
 

Awesome! I can't get over how easy this is going to make looking at complex Javascript objects. When I think of all the times that I've tried to create massive data strings for alert() calls by using a for-in object loop and string concatenation. So many wasted hours!

FireBug, you are amazing. Ray Camden, you rock - thanks for showing this to me.

NOTE: Running console.log( console ) doesn't do anything cool... it doesn't do anything at all, in fact.




Reader Comments

Jan 14, 2009 at 9:06 AM // reply »
2 Comments

Handy! I didn't know that. But if you weren't that organised to put the console.log() and console.dir() in your code to begin with, the variable will usually be accessible in the DOM tab of FireBug.


Jan 14, 2009 at 9:16 AM // reply »
30 Comments

The Firebug console is awesome. It actually has quite a few methods which are documented here: http://getfirebug.com/console.html


Jan 14, 2009 at 9:34 AM // reply »
11,238 Comments

@Alan,

Good to know - I didn't even know the DOM tab would include user-defined Javascript objects! Your comment got me thinking:

http://www.bennadel.com/index.cfm?dax=blog:1459.view

@Nathan,

Thanks for the link.


Jan 14, 2009 at 10:36 AM // reply »
14 Comments

Console is the bomb. An absolute must for anyone doing client-side development. Their is one downside, which we hit here this morning. Be careful to remove your console statements from code prior to pushing to production. Internet Explorer (go figure) does not support console, and the console statements will error your code and stop process.


Jan 14, 2009 at 10:38 AM // reply »
11,238 Comments

@Steve,

Good point. I guess, it's probably best just to use them when something is going wrong.


Jan 14, 2009 at 2:20 PM // reply »
25 Comments

Don't forget about the 'debugger;' statement that lets you dynamically add in break points for your code. I just found that out and have been loving it ever since.


Jan 14, 2009 at 3:02 PM // reply »
11,238 Comments

@JAlpino,

Mark Drew just walked me through some debugger stuff this morning. Looks really nice.


Jan 15, 2009 at 10:51 PM // reply »
57 Comments

Very cool, thanks for sharing! I've played a bit with the JS debugger in the past, but this will definitely make it much more useful.


Sep 9, 2011 at 6:57 PM // reply »
7 Comments

You have saved me once again! I was in the alert box hell, when I found this post. Thank you!!!


Aug 19, 2012 at 6:02 PM // reply »
1 Comments

One thing to note though.
Remove any console.log() code you have in your javascript before you go live.
Why?
Microsoft in their wonderful wisdom for IE9 will halt javascript functionality when it comes across console.log().
It will only run javascript fully if you boot up the IE9 developer thing you will then find it all just works as you expect.
So IE9 - Only reads console.log() when you got the developer window up (F12)
Crazy.


Aug 21, 2012 at 4:01 AM // reply »
1 Comments

@Liam,

One way I've gotten around this problem with IE9 and older browsers is to shim out the console object and the various methods it has - log(), dir(), whatever.

You get a consistent interface to use across your code without having to worry too much about the browser.


Oct 18, 2012 at 5:40 PM // reply »
1 Comments

Here's a handy snippet to enable IE to handle console.log() before the dev tools are opened, so you don't get those nasty errors:

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


Nov 6, 2012 at 9:32 PM // reply »
1 Comments

I downloaded the code as a text file (ending with .html) but I got only "FireBug Console Testing" with nothing in console. I use Firefox 16.0.1 and Firebug 1.10.6. I need this to work... Help!



Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 20, 2013 at 11:45 AM
Using jQuery's Animate() Step Callback Function To Create Custom Animations
This is really useful. I found out that you don't actually have to use a dummy css property (surprisingly). To animate a property in a linear-gradient for instance I did this this.css('someLinearGra ... read »
May 20, 2013 at 10:51 AM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Josh, Oh snap! You're totally right! I'm not sure I've ever tried that. I did know that you can call a number of other array-methods on ColdFusion query columns: http://www.bennadel.com/blog/167 ... read »
May 20, 2013 at 10:45 AM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Ben - I believe you can achieve the same functionality with ColdFusion's built in ArrayToList() function. ArrayToList( users[ "id" ] ); ... read »
May 20, 2013 at 10:21 AM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
Is there any error logging and handling framework in angularjs, if not then in what way I can do this. ... read »
May 19, 2013 at 2:31 PM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
It's funny really just how well that image describes the way I would imagine most people that go with angular for some project is. I have had a similar roller-coaster ride with it as well, but not qu ... read »
May 17, 2013 at 7:42 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
Ben - thanks so much for posting these Angular articles and findings, they've been a huge help towards learning one of the more 'complex' JavaScript frameworks out there (IMO). I have been using Angu ... read »
May 16, 2013 at 5:01 PM
UPDATE: Parsing CSV Data Files In ColdFusion With csvToArray()
Your code was the closest thing I've found to obtaining some direction for converting ISO fields to values that CF can translate properly. Thank you for posting! ... read »
May 15, 2013 at 6:07 PM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Ben, you once again saved my bacon at work. Thank you, thank you, thank you! ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools